use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class OperatorAssertion method toRow.
public static Block toRow(List<Type> parameterTypes, Object... values) {
checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
RowType rowType = RowType.anonymous(parameterTypes);
BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1);
BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < values.length; i++) {
appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
}
blockBuilder.closeEntry();
return rowType.getObject(blockBuilder, 0);
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class AbstractTestType method getNonNullValueForType.
/**
* @return a non-null value, represented in native container type
*/
private static Object getNonNullValueForType(Type type) {
if (type.getJavaType() == boolean.class) {
return true;
}
if (type.getJavaType() == long.class) {
return 1L;
}
if (type.getJavaType() == double.class) {
return 1.0;
}
if (type.getJavaType() == Slice.class) {
return Slices.utf8Slice("_");
}
if (type instanceof ArrayType) {
ArrayType arrayType = (ArrayType) type;
Type elementType = arrayType.getElementType();
Object elementNonNullValue = getNonNullValueForType(elementType);
return arrayBlockOf(elementType, elementNonNullValue);
}
if (type instanceof MapType) {
MapType mapType = (MapType) type;
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
Object keyNonNullValue = getNonNullValueForType(keyType);
Object valueNonNullValue = getNonNullValueForType(valueType);
Map map = ImmutableMap.of(keyNonNullValue, valueNonNullValue);
return mapBlockOf(keyType, valueType, map);
}
if (type instanceof RowType) {
RowType rowType = (RowType) type;
List<Type> elementTypes = rowType.getTypeParameters();
Object[] elementNonNullValues = elementTypes.stream().map(AbstractTestType::getNonNullValueForType).toArray(Object[]::new);
return toRow(elementTypes, elementNonNullValues);
}
throw new IllegalStateException("Unsupported Java type " + type.getJavaType() + " (for type " + type + ")");
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class OrcStorageManager method getColumnInfoFromOrcColumnTypes.
private List<ColumnInfo> getColumnInfoFromOrcColumnTypes(List<String> orcColumnNames, List<OrcType> orcColumnTypes) {
Type rowType = getType(orcColumnTypes, 0);
if (orcColumnNames.size() != rowType.getTypeParameters().size()) {
throw new PrestoException(RAPTOR_ERROR, "Column names and types do not match");
}
ImmutableList.Builder<ColumnInfo> list = ImmutableList.builder();
for (int i = 0; i < orcColumnNames.size(); i++) {
list.add(new ColumnInfo(Long.parseLong(orcColumnNames.get(i)), rowType.getTypeParameters().get(i)));
}
return list.build();
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class ArrayColumnValidator method generateArrayChecksum.
public static Expression generateArrayChecksum(Expression column, Type type) {
checkArgument(type instanceof ArrayType, "Expect ArrayType, found %s", type.getDisplayName());
Type elementType = ((ArrayType) type).getElementType();
if (elementType.isOrderable()) {
FunctionCall arraySort = new FunctionCall(QualifiedName.of("array_sort"), ImmutableList.of(column));
if (elementType instanceof ArrayType || elementType instanceof RowType) {
return new CoalesceExpression(new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(new TryExpression(arraySort))), new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(column)));
}
return new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(arraySort));
}
return new FunctionCall(QualifiedName.of("checksum"), ImmutableList.of(column));
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class H2QueryRunner method rowMapper.
private static RowMapper<MaterializedRow> rowMapper(List<? extends Type> types) {
return new RowMapper<MaterializedRow>() {
private Object getValue(Type type, ResultSet resultSet, int position) throws SQLException {
if (BOOLEAN.equals(type)) {
boolean booleanValue = resultSet.getBoolean(position);
return resultSet.wasNull() ? null : booleanValue;
} else if (TINYINT.equals(type)) {
byte byteValue = resultSet.getByte(position);
return resultSet.wasNull() ? null : byteValue;
} else if (SMALLINT.equals(type)) {
short shortValue = resultSet.getShort(position);
return resultSet.wasNull() ? null : shortValue;
} else if (INTEGER.equals(type)) {
int intValue = resultSet.getInt(position);
return resultSet.wasNull() ? null : intValue;
} else if (BIGINT.equals(type)) {
long longValue = resultSet.getLong(position);
return resultSet.wasNull() ? null : longValue;
} else if (REAL.equals(type)) {
float floatValue = resultSet.getFloat(position);
return resultSet.wasNull() ? null : floatValue;
} else if (DOUBLE.equals(type)) {
double doubleValue = resultSet.getDouble(position);
return resultSet.wasNull() ? null : doubleValue;
} else if (isVarcharType(type)) {
String stringValue = resultSet.getString(position);
return resultSet.wasNull() ? null : stringValue;
} else if (isCharType(type)) {
String stringValue = resultSet.getString(position);
return resultSet.wasNull() ? null : padEnd(stringValue, ((CharType) type).getLength(), ' ');
} else if (VARBINARY.equals(type)) {
byte[] binary = resultSet.getBytes(position);
return resultSet.wasNull() ? null : binary;
} 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(position, LocalDate.class);
return resultSet.wasNull() ? null : 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(position, LocalTime.class);
return resultSet.wasNull() ? null : 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(position, 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(position, LocalDate.class)).map(LocalDate::atStartOfDay).orElse(null);
} catch (RuntimeException e) {
first.addSuppressed(e);
throw first;
}
}
return resultSet.wasNull() ? null : 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(position);
checkState(resultSet.wasNull(), "Expected a null value, but got %s", objectValue);
return null;
} else if (type instanceof DecimalType) {
DecimalType decimalType = (DecimalType) type;
BigDecimal decimalValue = resultSet.getBigDecimal(position);
return resultSet.wasNull() ? null : decimalValue.setScale(decimalType.getScale(), BigDecimal.ROUND_HALF_UP).round(new MathContext(decimalType.getPrecision()));
} else if (type instanceof ArrayType) {
Array array = resultSet.getArray(position);
return resultSet.wasNull() ? null : newArrayList(mapArrayValues(((ArrayType) type), (Object[]) array.getArray()));
} else if (type instanceof RowType) {
Array array = resultSet.getArray(position);
return resultSet.wasNull() ? null : newArrayList(mapRowValues((RowType) type, (Object[]) array.getArray()));
} else if (type instanceof TypeWithName) {
return getValue(((TypeWithName) type).getType(), resultSet, position);
} else {
throw new AssertionError("unhandled type: " + type);
}
}
@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++) {
row.add(getValue(types.get(i - 1), resultSet, i));
}
return new MaterializedRow(MaterializedResult.DEFAULT_PRECISION, row);
}
};
}
Aggregations