use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class StructuralTestUtil method mapBlockOf.
public static Block mapBlockOf(Type keyType, Type valueType, Object[] keys, Object[] values) {
checkArgument(keys.length == values.length, "keys/values must have the same length");
MapType mapType = mapType(keyType, valueType);
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
BlockBuilder singleMapBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < keys.length; i++) {
Object key = keys[i];
Object value = values[i];
appendToBlockBuilder(keyType, key, singleMapBlockWriter);
appendToBlockBuilder(valueType, value, singleMapBlockWriter);
}
blockBuilder.closeEntry();
return mapType.getObject(blockBuilder, 0);
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class JsonToMapCast method specialize.
@Override
public BuiltInScalarFunctionImplementation specialize(BoundVariables boundVariables, int arity, FunctionAndTypeManager functionAndTypeManager) {
checkArgument(arity == 1, "Expected arity to be 1");
Type keyType = boundVariables.getTypeVariable("K");
Type valueType = boundVariables.getTypeVariable("V");
MapType mapType = (MapType) functionAndTypeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType.getTypeSignature()), TypeSignatureParameter.of(valueType.getTypeSignature())));
checkCondition(canCastFromJson(mapType), INVALID_CAST_ARGUMENT, "Cannot cast JSON to %s", mapType);
BlockBuilderAppender keyAppender = createBlockBuilderAppender(mapType.getKeyType());
BlockBuilderAppender valueAppender = createBlockBuilderAppender(mapType.getValueType());
MethodHandle methodHandle = METHOD_HANDLE.bindTo(mapType).bindTo(keyAppender).bindTo(valueAppender);
return new BuiltInScalarFunctionImplementation(true, ImmutableList.of(valueTypeArgumentProperty(RETURN_NULL_ON_NULL)), methodHandle);
}
use of io.prestosql.spi.type.MapType in project hetu-core by openlookeng.
the class MapConcatFunction method mapConcat.
@UsedByGeneratedCode
public static Block mapConcat(MapType mapType, Object state, Block[] maps) {
int entries = 0;
int lastMapIndex = maps.length - 1;
int firstMapIndex = lastMapIndex;
for (int i = 0; i < maps.length; i++) {
entries += maps[i].getPositionCount();
if (maps[i].getPositionCount() > 0) {
lastMapIndex = i;
firstMapIndex = min(firstMapIndex, i);
}
}
if (lastMapIndex == firstMapIndex) {
return maps[lastMapIndex];
}
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
// TODO: we should move TypedSet into user state as well
Type keyType = mapType.getKeyType();
Type valueType = mapType.getValueType();
TypedSet typedSet = new TypedSet(keyType, entries / 2, FUNCTION_NAME);
BlockBuilder mapBlockBuilder = pageBuilder.getBlockBuilder(0);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
// the last map
Block map = maps[lastMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
typedSet.add(map, i);
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
// the map between the last and the first
for (int idx = lastMapIndex - 1; idx > firstMapIndex; idx--) {
map = maps[idx];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
typedSet.add(map, i);
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
}
// the first map
map = maps[firstMapIndex];
for (int i = 0; i < map.getPositionCount(); i += 2) {
if (!typedSet.contains(map, i)) {
keyType.appendTo(map, i, blockBuilder);
valueType.appendTo(map, i + 1, blockBuilder);
}
}
mapBlockBuilder.closeEntry();
pageBuilder.declarePosition();
return mapType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
use of io.prestosql.spi.type.MapType 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.MapType in project hetu-core by openlookeng.
the class BenchmarkMapCopy method benchmarkMapCopy.
@Benchmark
@OperationsPerInvocation(POSITIONS)
public BlockBuilder benchmarkMapCopy(BenchmarkData data) {
Block block = data.getDataBlock();
BlockBuilder blockBuilder = data.getBlockBuilder();
MapType mapType = mapType(VARCHAR, BIGINT);
for (int i = 0; i < POSITIONS; i++) {
mapType.appendTo(block, i, blockBuilder);
}
return blockBuilder;
}
Aggregations