use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class TestRowOperators method assertRowHashOperator.
private void assertRowHashOperator(String inputString, List<Type> types, List<Object> elements) {
checkArgument(types.size() == elements.size(), "types and elements must have the same size");
RowType rowType = RowType.anonymous(types);
BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < types.size(); i++) {
appendToBlockBuilder(types.get(i), elements.get(i), singleRowBlockWriter);
}
blockBuilder.closeEntry();
assertOperator(HASH_CODE, inputString, BIGINT, rowType.hash(blockBuilder.build(), 0));
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class RowExpressionVerifier method visitDereferenceExpression.
@Override
protected Boolean visitDereferenceExpression(DereferenceExpression expected, RowExpression actual) {
if (!(actual instanceof SpecialForm) || !(((SpecialForm) actual).getForm().equals(DEREFERENCE))) {
return false;
}
SpecialForm actualDereference = (SpecialForm) actual;
if (actualDereference.getArguments().size() == 2 && actualDereference.getArguments().get(0).getType() instanceof RowType && actualDereference.getArguments().get(1) instanceof ConstantExpression) {
RowType rowType = (RowType) actualDereference.getArguments().get(0).getType();
Object value = LiteralInterpreter.evaluate((ConstantExpression) actualDereference.getArguments().get(1));
checkState(value instanceof Long);
long index = (Long) value;
checkState(index >= 0 && index < rowType.getFields().size());
RowType.Field field = rowType.getFields().get(toIntExact(index));
checkState(field.getName().isPresent());
return expected.getField().getValue().equals(field.getName().get()) && process(expected.getBase(), actualDereference.getArguments().get(0));
}
return false;
}
use of io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
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 io.prestosql.spi.type.RowType in project hetu-core by openlookeng.
the class JsonToRowCast method specialize.
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
checkArgument(arity == 1, "Expected arity to be 1");
RowType rowType = (RowType) boundVariables.getTypeVariable("T");
checkCondition(canCastFromJson(rowType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", rowType);
List<Field> rowFields = rowType.getFields();
BlockBuilderAppender[] fieldAppenders = rowFields.stream().map(rowField -> createBlockBuilderAppender(rowField.getType())).toArray(BlockBuilderAppender[]::new);
MethodHandle methodHandle = METHOD_HANDLE.bindTo(rowType).bindTo(fieldAppenders).bindTo(getFieldNameToIndex(rowFields));
return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
use of io.prestosql.spi.type.RowType 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);
}
}
Aggregations