Search in sources :

Example 21 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class QueryBuilder method buildSql.

public PreparedStatement buildSql(JdbcClient client, Connection connection, String catalog, String schema, String table, List<JdbcColumnHandle> columns, TupleDomain<ColumnHandle> tupleDomain) throws SQLException {
    StringBuilder sql = new StringBuilder();
    String columnNames = columns.stream().map(JdbcColumnHandle::getColumnName).map(this::quote).collect(joining(", "));
    sql.append("SELECT ");
    sql.append(columnNames);
    if (columns.isEmpty()) {
        sql.append("null");
    }
    sql.append(" FROM ");
    if (!isNullOrEmpty(catalog)) {
        sql.append(quote(catalog)).append('.');
    }
    if (!isNullOrEmpty(schema)) {
        sql.append(quote(schema)).append('.');
    }
    sql.append(quote(table));
    List<TypeAndValue> accumulator = new ArrayList<>();
    List<String> clauses = toConjuncts(columns, tupleDomain, accumulator);
    if (!clauses.isEmpty()) {
        sql.append(" WHERE ").append(Joiner.on(" AND ").join(clauses));
    }
    PreparedStatement statement = client.getPreparedStatement(connection, sql.toString());
    for (int i = 0; i < accumulator.size(); i++) {
        TypeAndValue typeAndValue = accumulator.get(i);
        if (typeAndValue.getType().equals(BigintType.BIGINT)) {
            statement.setLong(i + 1, (long) typeAndValue.getValue());
        } else if (typeAndValue.getType().equals(IntegerType.INTEGER)) {
            statement.setInt(i + 1, ((Number) typeAndValue.getValue()).intValue());
        } else if (typeAndValue.getType().equals(SmallintType.SMALLINT)) {
            statement.setShort(i + 1, ((Number) typeAndValue.getValue()).shortValue());
        } else if (typeAndValue.getType().equals(TinyintType.TINYINT)) {
            statement.setByte(i + 1, ((Number) typeAndValue.getValue()).byteValue());
        } else if (typeAndValue.getType().equals(DoubleType.DOUBLE)) {
            statement.setDouble(i + 1, (double) typeAndValue.getValue());
        } else if (typeAndValue.getType().equals(RealType.REAL)) {
            statement.setFloat(i + 1, intBitsToFloat(((Number) typeAndValue.getValue()).intValue()));
        } else if (typeAndValue.getType().equals(BooleanType.BOOLEAN)) {
            statement.setBoolean(i + 1, (boolean) typeAndValue.getValue());
        } else if (typeAndValue.getType().equals(DateType.DATE)) {
            long millis = DAYS.toMillis((long) typeAndValue.getValue());
            statement.setDate(i + 1, new Date(UTC.getMillisKeepLocal(DateTimeZone.getDefault(), millis)));
        } else if (typeAndValue.getType().equals(TimeType.TIME)) {
            statement.setTime(i + 1, new Time((long) typeAndValue.getValue()));
        } else if (typeAndValue.getType().equals(TimeWithTimeZoneType.TIME_WITH_TIME_ZONE)) {
            statement.setTime(i + 1, new Time(unpackMillisUtc((long) typeAndValue.getValue())));
        } else if (typeAndValue.getType().equals(TimestampType.TIMESTAMP)) {
            statement.setTimestamp(i + 1, new Timestamp((long) typeAndValue.getValue()));
        } else if (typeAndValue.getType().equals(TimestampWithTimeZoneType.TIMESTAMP_WITH_TIME_ZONE)) {
            statement.setTimestamp(i + 1, new Timestamp(unpackMillisUtc((long) typeAndValue.getValue())));
        } else if (typeAndValue.getType() instanceof VarcharType) {
            statement.setString(i + 1, ((Slice) typeAndValue.getValue()).toStringUtf8());
        } else {
            throw new UnsupportedOperationException("Can't handle type: " + typeAndValue.getType());
        }
    }
    return statement;
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) Time(java.sql.Time) Timestamp(java.sql.Timestamp) Date(java.sql.Date)

Example 22 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class SequencePageBuilder method createSequencePage.

public static Page createSequencePage(List<? extends Type> types, int length, int... initialValues) {
    Block[] blocks = new Block[initialValues.length];
    for (int i = 0; i < blocks.length; i++) {
        Type type = types.get(i);
        int initialValue = initialValues[i];
        if (type.equals(BIGINT)) {
            blocks[i] = BlockAssertions.createLongSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(REAL)) {
            blocks[i] = BlockAssertions.createSequenceBlockOfReal(initialValue, initialValue + length);
        } else if (type.equals(DOUBLE)) {
            blocks[i] = BlockAssertions.createDoubleSequenceBlock(initialValue, initialValue + length);
        } else if (type instanceof VarcharType) {
            blocks[i] = BlockAssertions.createStringSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(BOOLEAN)) {
            blocks[i] = BlockAssertions.createBooleanSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(DATE)) {
            blocks[i] = BlockAssertions.createDateSequenceBlock(initialValue, initialValue + length);
        } else if (type.equals(TIMESTAMP)) {
            blocks[i] = BlockAssertions.createTimestampSequenceBlock(initialValue, initialValue + length);
        } else if (isShortDecimal(type)) {
            blocks[i] = BlockAssertions.createShortDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
        } else if (isLongDecimal(type)) {
            blocks[i] = BlockAssertions.createLongDecimalSequenceBlock(initialValue, initialValue + length, (DecimalType) type);
        } else {
            throw new IllegalStateException("Unsupported type " + type);
        }
    }
    return new Page(blocks);
}
Also used : DecimalType(com.facebook.presto.spi.type.DecimalType) Type(com.facebook.presto.spi.type.Type) VarcharType(com.facebook.presto.spi.type.VarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Block(com.facebook.presto.spi.block.Block) DecimalType(com.facebook.presto.spi.type.DecimalType) Page(com.facebook.presto.spi.Page)

Example 23 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class TestInterleavedBlock method testGetSizeInBytes.

@Test
private void testGetSizeInBytes() {
    int numEntries = 2000;
    VarcharType unboundedVarcharType = createUnboundedVarcharType();
    InterleavedBlockBuilder blockBuilder = new InterleavedBlockBuilder(ImmutableList.of(unboundedVarcharType, BIGINT), new BlockBuilderStatus(), numEntries);
    for (int i = 0; i < numEntries; i += 2) {
        unboundedVarcharType.writeString(blockBuilder, String.valueOf(ThreadLocalRandom.current().nextLong()));
        BIGINT.writeLong(blockBuilder, ThreadLocalRandom.current().nextLong());
    }
    InterleavedBlock block = blockBuilder.build();
    List<Block> splitQuarter = splitBlock(block, 4);
    int sizeInBytes = block.getSizeInBytes();
    int quarter1size = splitQuarter.get(0).getSizeInBytes();
    int quarter2size = splitQuarter.get(1).getSizeInBytes();
    int quarter3size = splitQuarter.get(2).getSizeInBytes();
    int quarter4size = splitQuarter.get(3).getSizeInBytes();
    double expectedQuarterSizeMin = sizeInBytes * 0.2;
    double expectedQuarterSizeMax = sizeInBytes * 0.3;
    assertTrue(quarter1size > expectedQuarterSizeMin && quarter1size < expectedQuarterSizeMax, format("quarter1size is %s, should be between %s and %s", quarter1size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter2size > expectedQuarterSizeMin && quarter2size < expectedQuarterSizeMax, format("quarter2size is %s, should be between %s and %s", quarter2size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter3size > expectedQuarterSizeMin && quarter3size < expectedQuarterSizeMax, format("quarter3size is %s, should be between %s and %s", quarter3size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter4size > expectedQuarterSizeMin && quarter4size < expectedQuarterSizeMax, format("quarter4size is %s, should be between %s and %s", quarter4size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertEquals(quarter1size + quarter2size + quarter3size + quarter4size, sizeInBytes);
}
Also used : VarcharType.createUnboundedVarcharType(com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType) VarcharType(com.facebook.presto.spi.type.VarcharType) Block(com.facebook.presto.spi.block.Block) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) InterleavedBlockBuilder(com.facebook.presto.spi.block.InterleavedBlockBuilder) InterleavedBlock(com.facebook.presto.spi.block.InterleavedBlock) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) Test(org.testng.annotations.Test)

Example 24 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class TestMathFunctions method testToBase.

@Test
public void testToBase() throws Exception {
    VarcharType toBaseReturnType = VarcharType.createVarcharType(64);
    assertFunction("to_base(2147483648, 16)", toBaseReturnType, "80000000");
    assertFunction("to_base(255, 2)", toBaseReturnType, "11111111");
    assertFunction("to_base(-2147483647, 16)", toBaseReturnType, "-7fffffff");
    assertFunction("to_base(NULL, 16)", toBaseReturnType, null);
    assertFunction("to_base(-2147483647, NULL)", toBaseReturnType, null);
    assertFunction("to_base(NULL, NULL)", toBaseReturnType, null);
    assertInvalidFunction("to_base(255, 1)", "Radix must be between 2 and 36");
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) Test(org.testng.annotations.Test)

Example 25 with VarcharType

use of com.facebook.presto.spi.type.VarcharType in project presto by prestodb.

the class HiveTypeTranslator method translate.

@Override
public TypeInfo translate(Type type) {
    if (BOOLEAN.equals(type)) {
        return HIVE_BOOLEAN.getTypeInfo();
    }
    if (BIGINT.equals(type)) {
        return HIVE_LONG.getTypeInfo();
    }
    if (INTEGER.equals(type)) {
        return HIVE_INT.getTypeInfo();
    }
    if (SMALLINT.equals(type)) {
        return HIVE_SHORT.getTypeInfo();
    }
    if (TINYINT.equals(type)) {
        return HIVE_BYTE.getTypeInfo();
    }
    if (REAL.equals(type)) {
        return HIVE_FLOAT.getTypeInfo();
    }
    if (DOUBLE.equals(type)) {
        return HIVE_DOUBLE.getTypeInfo();
    }
    if (type instanceof VarcharType) {
        VarcharType varcharType = (VarcharType) type;
        int varcharLength = varcharType.getLength();
        if (varcharLength <= HiveVarchar.MAX_VARCHAR_LENGTH) {
            return getVarcharTypeInfo(varcharLength);
        } else if (varcharLength == VarcharType.UNBOUNDED_LENGTH) {
            return HIVE_STRING.getTypeInfo();
        } else {
            throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported VARCHAR types: VARCHAR(<=%d), VARCHAR.", type, HiveVarchar.MAX_VARCHAR_LENGTH));
        }
    }
    if (type instanceof CharType) {
        CharType charType = (CharType) type;
        int charLength = charType.getLength();
        if (charLength <= HiveChar.MAX_CHAR_LENGTH) {
            return getCharTypeInfo(charLength);
        }
        throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s. Supported CHAR types: CHAR(<=%d).", type, HiveChar.MAX_CHAR_LENGTH));
    }
    if (VARBINARY.equals(type)) {
        return HIVE_BINARY.getTypeInfo();
    }
    if (DATE.equals(type)) {
        return HIVE_DATE.getTypeInfo();
    }
    if (TIMESTAMP.equals(type)) {
        return HIVE_TIMESTAMP.getTypeInfo();
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale());
    }
    if (isArrayType(type)) {
        TypeInfo elementType = translate(type.getTypeParameters().get(0));
        return getListTypeInfo(elementType);
    }
    if (isMapType(type)) {
        TypeInfo keyType = translate(type.getTypeParameters().get(0));
        TypeInfo valueType = translate(type.getTypeParameters().get(1));
        return getMapTypeInfo(keyType, valueType);
    }
    if (isRowType(type)) {
        ImmutableList.Builder<String> fieldNames = ImmutableList.builder();
        for (TypeSignatureParameter parameter : type.getTypeSignature().getParameters()) {
            if (!parameter.isNamedTypeSignature()) {
                throw new IllegalArgumentException(format("Expected all parameters to be named type, but got %s", parameter));
            }
            NamedTypeSignature namedTypeSignature = parameter.getNamedTypeSignature();
            fieldNames.add(namedTypeSignature.getName());
        }
        return getStructTypeInfo(fieldNames.build(), type.getTypeParameters().stream().map(this::translate).collect(toList()));
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported Hive type: %s", type));
}
Also used : VarcharType(com.facebook.presto.spi.type.VarcharType) ImmutableList(com.google.common.collect.ImmutableList) NamedTypeSignature(com.facebook.presto.spi.type.NamedTypeSignature) PrestoException(com.facebook.presto.spi.PrestoException) TypeInfoFactory.getCharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getCharTypeInfo) TypeInfoFactory.getStructTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getStructTypeInfo) TypeInfoFactory.getVarcharTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getVarcharTypeInfo) TypeInfoFactory.getMapTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getMapTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfo) TypeInfoFactory.getListTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.TypeInfoFactory.getListTypeInfo) DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) TypeSignatureParameter(com.facebook.presto.spi.type.TypeSignatureParameter) DecimalType(com.facebook.presto.spi.type.DecimalType) CharType(com.facebook.presto.spi.type.CharType)

Aggregations

VarcharType (com.facebook.presto.spi.type.VarcharType)25 Type (com.facebook.presto.spi.type.Type)14 DecimalType (com.facebook.presto.spi.type.DecimalType)10 PrestoException (com.facebook.presto.spi.PrestoException)9 Block (com.facebook.presto.spi.block.Block)8 CharType (com.facebook.presto.spi.type.CharType)7 ArrayList (java.util.ArrayList)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType)6 Chars.isCharType (com.facebook.presto.spi.type.Chars.isCharType)5 Date (java.sql.Date)5 Timestamp (java.sql.Timestamp)5 BlockBuilderStatus (com.facebook.presto.spi.block.BlockBuilderStatus)4 ImmutableList (com.google.common.collect.ImmutableList)4 Slice (io.airlift.slice.Slice)4 HashMap (java.util.HashMap)4 List (java.util.List)4 BlockBuilder (com.facebook.presto.spi.block.BlockBuilder)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Time (java.sql.Time)3 DecimalTypeInfo (org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo)3