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);
}
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);
}
}
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 (?,?)");
}
}
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();
}
}
}
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);
}
Aggregations