Search in sources :

Example 11 with CharType

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

the class HiveWriteUtils method getJavaObjectInspector.

public static ObjectInspector getJavaObjectInspector(Type type) {
    if (type.equals(BooleanType.BOOLEAN)) {
        return javaBooleanObjectInspector;
    }
    if (type.equals(BigintType.BIGINT)) {
        return javaLongObjectInspector;
    }
    if (type.equals(IntegerType.INTEGER)) {
        return javaIntObjectInspector;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return javaShortObjectInspector;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return javaByteObjectInspector;
    }
    if (type.equals(RealType.REAL)) {
        return javaFloatObjectInspector;
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return javaDoubleObjectInspector;
    }
    if (type instanceof VarcharType) {
        return writableStringObjectInspector;
    }
    if (type instanceof CharType) {
        return writableHiveCharObjectInspector;
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return javaByteArrayObjectInspector;
    }
    if (type.equals(DateType.DATE)) {
        return javaDateObjectInspector;
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return javaTimestampObjectInspector;
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return getPrimitiveJavaObjectInspector(new DecimalTypeInfo(decimalType.getPrecision(), decimalType.getScale()));
    }
    if (isArrayType(type)) {
        return ObjectInspectorFactory.getStandardListObjectInspector(getJavaObjectInspector(type.getTypeParameters().get(0)));
    }
    if (isMapType(type)) {
        ObjectInspector keyObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(0));
        ObjectInspector valueObjectInspector = getJavaObjectInspector(type.getTypeParameters().get(1));
        return ObjectInspectorFactory.getStandardMapObjectInspector(keyObjectInspector, valueObjectInspector);
    }
    if (isRowType(type)) {
        return ObjectInspectorFactory.getStandardStructObjectInspector(type.getTypeSignature().getParameters().stream().map(parameter -> parameter.getNamedTypeSignature().getName().get()).collect(toList()), type.getTypeParameters().stream().map(HiveWriteUtils::getJavaObjectInspector).collect(toList()));
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
Also used : DecimalTypeInfo(org.apache.hadoop.hive.serde2.typeinfo.DecimalTypeInfo) 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) PrimitiveObjectInspectorFactory.writableTimestampObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableTimestampObjectInspector) 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) PrimitiveObjectInspectorFactory.writableFloatObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableFloatObjectInspector) PrimitiveObjectInspectorFactory.javaShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaShortObjectInspector) PrimitiveObjectInspectorFactory.writableBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBooleanObjectInspector) PrimitiveObjectInspectorFactory.writableBinaryObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableBinaryObjectInspector) PrimitiveObjectInspectorFactory.writableDoubleObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDoubleObjectInspector) PrimitiveObjectInspectorFactory.writableLongObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableLongObjectInspector) PrimitiveObjectInspectorFactory.writableByteObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableByteObjectInspector) PrimitiveObjectInspectorFactory.writableDateObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableDateObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveWritableObjectInspector) PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableHiveCharObjectInspector) PrimitiveObjectInspectorFactory.writableShortObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableShortObjectInspector) PrimitiveObjectInspectorFactory.javaBooleanObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.javaBooleanObjectInspector) PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.getPrimitiveJavaObjectInspector) PrimitiveObjectInspectorFactory.writableStringObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableStringObjectInspector) PrimitiveObjectInspectorFactory.writableIntObjectInspector(org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorFactory.writableIntObjectInspector) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) Chars.isCharType(io.prestosql.spi.type.Chars.isCharType) CharType(io.prestosql.spi.type.CharType)

Example 12 with CharType

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

the class MaterializedResult method writeValue.

private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
    if (value == null) {
        blockBuilder.appendNull();
    } else if (BIGINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
    } else if (INTEGER.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).intValue());
    } else if (SMALLINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).shortValue());
    } else if (TINYINT.equals(type)) {
        type.writeLong(blockBuilder, ((Number) value).byteValue());
    } else if (REAL.equals(type)) {
        type.writeLong(blockBuilder, (long) floatToRawIntBits(((Number) value).floatValue()));
    } else if (DOUBLE.equals(type)) {
        type.writeDouble(blockBuilder, ((Number) value).doubleValue());
    } else if (BOOLEAN.equals(type)) {
        type.writeBoolean(blockBuilder, (Boolean) value);
    } else if (JSON.equals(type)) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof VarcharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (type instanceof CharType) {
        type.writeSlice(blockBuilder, Slices.utf8Slice((String) value));
    } else if (VARBINARY.equals(type)) {
        type.writeSlice(blockBuilder, Slices.wrappedBuffer((byte[]) value));
    } else if (DATE.equals(type)) {
        int days = ((SqlDate) value).getDays();
        type.writeLong(blockBuilder, days);
    } else if (TIME.equals(type)) {
        SqlTime time = (SqlTime) value;
        if (time.isLegacyTimestamp()) {
            type.writeLong(blockBuilder, time.getMillisUtc());
        } else {
            type.writeLong(blockBuilder, time.getMillis());
        }
    } else if (TIME_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimeWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimeWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (TIMESTAMP.equals(type)) {
        long millisUtc = ((SqlTimestamp) value).getMillis();
        type.writeLong(blockBuilder, millisUtc);
    } else if (TIMESTAMP_WITH_TIME_ZONE.equals(type)) {
        long millisUtc = ((SqlTimestampWithTimeZone) value).getMillisUtc();
        TimeZoneKey timeZoneKey = ((SqlTimestampWithTimeZone) value).getTimeZoneKey();
        type.writeLong(blockBuilder, packDateTimeWithZone(millisUtc, timeZoneKey));
    } else if (ARRAY.equals(type.getTypeSignature().getBase())) {
        List<Object> list = (List<Object>) value;
        Type elementType = ((ArrayType) type).getElementType();
        BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object element : list) {
            writeValue(elementType, arrayBlockBuilder, element);
        }
        blockBuilder.closeEntry();
    } else if (MAP.equals(type.getTypeSignature().getBase())) {
        Map<Object, Object> map = (Map<Object, Object>) value;
        Type keyType = ((MapType) type).getKeyType();
        Type valueType = ((MapType) type).getValueType();
        BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<Object, Object> entry : map.entrySet()) {
            writeValue(keyType, mapBlockBuilder, entry.getKey());
            writeValue(valueType, mapBlockBuilder, entry.getValue());
        }
        blockBuilder.closeEntry();
    } else if (type instanceof RowType) {
        List<Object> row = (List<Object>) value;
        List<Type> fieldTypes = type.getTypeParameters();
        BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
        for (int field = 0; field < row.size(); field++) {
            writeValue(fieldTypes.get(field), rowBlockBuilder, row.get(field));
        }
        blockBuilder.closeEntry();
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) SqlTime(io.prestosql.spi.type.SqlTime) SqlTimeWithTimeZone(io.prestosql.spi.type.SqlTimeWithTimeZone) RowType(io.prestosql.spi.type.RowType) MapType(io.prestosql.spi.type.MapType) RowType(io.prestosql.spi.type.RowType) Type(io.prestosql.spi.type.Type) ArrayType(io.prestosql.spi.type.ArrayType) CharType(io.prestosql.spi.type.CharType) MapType(io.prestosql.spi.type.MapType) VarcharType(io.prestosql.spi.type.VarcharType) SqlTimestampWithTimeZone(io.prestosql.spi.type.SqlTimestampWithTimeZone) SqlDate(io.prestosql.spi.type.SqlDate) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) CharType(io.prestosql.spi.type.CharType) TimeZoneKey(io.prestosql.spi.type.TimeZoneKey) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) BlockBuilder(io.prestosql.spi.block.BlockBuilder)

Example 13 with CharType

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

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(), identity());
        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(io.prestosql.spi.connector.ColumnHandle) ImmutableSet(com.google.common.collect.ImmutableSet) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CharType(io.prestosql.spi.type.CharType) Test(org.testng.annotations.Test)

Example 14 with CharType

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

the class TestJdbcQueryBuilder method setup.

@BeforeMethod
public void setup() throws SQLException {
    database = new TestingDatabase();
    jdbcClient = database.getJdbcClient();
    CharType charType = CharType.createCharType(0);
    columns = ImmutableList.of(new JdbcColumnHandle("col_0", JDBC_BIGINT, BIGINT, true), new JdbcColumnHandle("col_1", JDBC_DOUBLE, DOUBLE, true), new JdbcColumnHandle("col_2", JDBC_BOOLEAN, BOOLEAN, true), new JdbcColumnHandle("col_3", JDBC_VARCHAR, VARCHAR, true), new JdbcColumnHandle("col_4", JDBC_DATE, DATE, true), new JdbcColumnHandle("col_5", JDBC_TIME, TIME, true), new JdbcColumnHandle("col_6", JDBC_TIMESTAMP, TIMESTAMP, true), new JdbcColumnHandle("col_7", JDBC_TINYINT, TINYINT, true), new JdbcColumnHandle("col_8", JDBC_SMALLINT, SMALLINT, true), new JdbcColumnHandle("col_9", JDBC_INTEGER, INTEGER, true), new JdbcColumnHandle("col_10", JDBC_REAL, REAL, true), new JdbcColumnHandle("col_11", JDBC_CHAR, charType, true));
    Connection connection = database.getConnection();
    try (PreparedStatement preparedStatement = connection.prepareStatement("create table \"test_table\" (" + "" + "\"col_0\" BIGINT, " + "\"col_1\" DOUBLE, " + "\"col_2\" BOOLEAN, " + "\"col_3\" VARCHAR(128), " + "\"col_4\" DATE, " + "\"col_5\" TIME, " + "\"col_6\" TIMESTAMP, " + "\"col_7\" TINYINT, " + "\"col_8\" SMALLINT, " + "\"col_9\" INTEGER, " + "\"col_10\" REAL, " + "\"col_11\" CHAR(128) " + ")")) {
        preparedStatement.execute();
        StringBuilder stringBuilder = new StringBuilder("insert into \"test_table\" values ");
        int len = 1000;
        LocalDateTime dateTime = LocalDateTime.of(2016, 3, 23, 12, 23, 37);
        for (int i = 0; i < len; i++) {
            stringBuilder.append(format(Locale.ENGLISH, "(%d, %f, %b, 'test_str_%d', '%s', '%s', '%s', %d, %d, %d, %f, 'test_str_%d')", i, 200000.0 + i / 2.0, i % 2 == 0, i, Date.valueOf(dateTime.toLocalDate()), Time.valueOf(dateTime.toLocalTime()), Timestamp.valueOf(dateTime), i % 128, -i, i - 100, 100.0f + i, i));
            dateTime = dateTime.plusHours(26);
            if (i != len - 1) {
                stringBuilder.append(",");
            }
        }
        try (PreparedStatement preparedStatement2 = connection.prepareStatement(stringBuilder.toString())) {
            preparedStatement2.execute();
        }
    }
}
Also used : LocalDateTime(java.time.LocalDateTime) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) CharType(io.prestosql.spi.type.CharType) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 15 with CharType

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

the class TestTypeUtil method testParametricType.

@Test
public void testParametricType() {
    Type type = parseType(typeManager, "decimal(10,2)");
    assertTrue(type instanceof DecimalType);
    type = parseType(typeManager, "char(100)");
    assertTrue(type instanceof CharType);
    type = parseType(typeManager, "varchar(100)");
    assertTrue(type instanceof VarcharType);
    type = parseType(typeManager, "array(varchar(10))");
    assertTrue(type instanceof ArrayType);
    type = parseType(typeManager, "row(street varchar(10))");
    assertTrue(type instanceof RowType);
}
Also used : ArrayType(io.prestosql.spi.type.ArrayType) BigintType(io.prestosql.spi.type.BigintType) UnknownType(io.prestosql.spi.type.UnknownType) CharType(io.prestosql.spi.type.CharType) DecimalType(io.prestosql.spi.type.DecimalType) HyperLogLogType(io.prestosql.spi.type.HyperLogLogType) DoubleType(io.prestosql.spi.type.DoubleType) P4HyperLogLogType(io.prestosql.spi.type.P4HyperLogLogType) RowType(io.prestosql.spi.type.RowType) TimestampWithTimeZoneType(io.prestosql.spi.type.TimestampWithTimeZoneType) Type(io.prestosql.spi.type.Type) SmallintType(io.prestosql.spi.type.SmallintType) TimeWithTimeZoneType(io.prestosql.spi.type.TimeWithTimeZoneType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) RealType(io.prestosql.spi.type.RealType) ArrayType(io.prestosql.spi.type.ArrayType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) TypeUtil.parseType(io.prestosql.client.util.TypeUtil.parseType) VarcharType(io.prestosql.spi.type.VarcharType) VarcharType(io.prestosql.spi.type.VarcharType) DecimalType(io.prestosql.spi.type.DecimalType) RowType(io.prestosql.spi.type.RowType) CharType(io.prestosql.spi.type.CharType) Test(org.testng.annotations.Test)

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