use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.
the class MapSubscriptOperator method subscript.
@UsedByGeneratedCode
public static Object subscript(MissingKeyExceptionFactory missingKeyExceptionFactory, Type keyType, Type valueType, ConnectorSession session, Block map, Object key) {
SingleMapBlock mapBlock = (SingleMapBlock) map;
int valuePosition = mapBlock.seekKeyExact(key);
if (valuePosition == -1) {
throw missingKeyExceptionFactory.create(session, key);
}
return readNativeValue(valueType, mapBlock, valuePosition);
}
use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.
the class MapSubscriptOperator method subscript.
@UsedByGeneratedCode
public static Object subscript(MissingKeyExceptionFactory missingKeyExceptionFactory, Type keyType, Type valueType, ConnectorSession session, Block map, double key) {
SingleMapBlock mapBlock = (SingleMapBlock) map;
int valuePosition = mapBlock.seekKeyExact(key);
if (valuePosition == -1) {
throw missingKeyExceptionFactory.create(session, key);
}
return readNativeValue(valueType, mapBlock, valuePosition);
}
use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.
the class MapSubscriptOperator method subscript.
@UsedByGeneratedCode
public static Object subscript(MissingKeyExceptionFactory missingKeyExceptionFactory, Type keyType, Type valueType, ConnectorSession session, Block map, long key) {
SingleMapBlock mapBlock = (SingleMapBlock) map;
int valuePosition = mapBlock.seekKeyExact(key);
if (valuePosition == -1) {
throw missingKeyExceptionFactory.create(session, key);
}
return readNativeValue(valueType, mapBlock, valuePosition);
}
use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.
the class MapToJsonCast method toJson.
@UsedByGeneratedCode
public static Slice toJson(ObjectKeyProvider provider, JsonGeneratorWriter writer, Block block) {
try {
Map<String, Integer> orderedKeyToValuePosition = new TreeMap<>();
for (int i = 0; i < block.getPositionCount(); i += 2) {
String objectKey = provider.getObjectKey(block, i);
orderedKeyToValuePosition.put(objectKey, i + 1);
}
SliceOutput output = new DynamicSliceOutput(40);
try (JsonGenerator jsonGenerator = createJsonGenerator(JSON_FACTORY, output)) {
jsonGenerator.writeStartObject();
for (Map.Entry<String, Integer> entry : orderedKeyToValuePosition.entrySet()) {
jsonGenerator.writeFieldName(entry.getKey());
writer.writeJsonValue(jsonGenerator, block, entry.getValue());
}
jsonGenerator.writeEndObject();
}
return output.slice();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
use of io.trino.annotation.UsedByGeneratedCode in project trino by trinodb.
the class MapToMapCast method mapCast.
@UsedByGeneratedCode
public static Block mapCast(MethodHandle keyProcessFunction, MethodHandle valueProcessFunction, Type targetType, BlockPositionEqual keyEqual, BlockPositionHashCode keyHashCode, ConnectorSession session, Block fromMap) {
checkState(targetType.getTypeParameters().size() == 2, "Expect two type parameters for targetType");
Type toKeyType = targetType.getTypeParameters().get(0);
TypedSet resultKeys = createEqualityTypedSet(toKeyType, keyEqual, keyHashCode, fromMap.getPositionCount() / 2, "map-to-map cast");
// Cast the keys into a new block
BlockBuilder keyBlockBuilder = toKeyType.createBlockBuilder(null, fromMap.getPositionCount() / 2);
for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
try {
keyProcessFunction.invokeExact(fromMap, i, session, keyBlockBuilder);
} catch (Throwable t) {
throw internalError(t);
}
}
Block keyBlock = keyBlockBuilder.build();
BlockBuilder mapBlockBuilder = targetType.createBlockBuilder(null, 1);
BlockBuilder blockBuilder = mapBlockBuilder.beginBlockEntry();
for (int i = 0; i < fromMap.getPositionCount(); i += 2) {
if (resultKeys.add(keyBlock, i / 2)) {
toKeyType.appendTo(keyBlock, i / 2, blockBuilder);
if (fromMap.isNull(i + 1)) {
blockBuilder.appendNull();
continue;
}
try {
valueProcessFunction.invokeExact(fromMap, i + 1, session, blockBuilder);
} catch (Throwable t) {
throw internalError(t);
}
} else {
// if there are duplicated keys, fail it!
throw new TrinoException(INVALID_CAST_ARGUMENT, "duplicate keys");
}
}
mapBlockBuilder.closeEntry();
return (Block) targetType.getObject(mapBlockBuilder, mapBlockBuilder.getPositionCount() - 1);
}
Aggregations