use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class RelationPlanner method visitUnnest.
@Override
protected RelationPlan visitUnnest(Unnest node, Void context) {
Scope scope = analysis.getScope(node);
ImmutableList.Builder<VariableReferenceExpression> outputVariablesBuilder = ImmutableList.builder();
for (Field field : scope.getRelationType().getVisibleFields()) {
VariableReferenceExpression variable = variableAllocator.newVariable(field);
outputVariablesBuilder.add(variable);
}
List<VariableReferenceExpression> unnestedVariables = outputVariablesBuilder.build();
// If we got here, then we must be unnesting a constant, and not be in a join (where there could be column references)
ImmutableList.Builder<VariableReferenceExpression> argumentVariables = ImmutableList.builder();
ImmutableList.Builder<RowExpression> values = ImmutableList.builder();
ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
Iterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
for (Expression expression : node.getExpressions()) {
Type type = analysis.getType(expression);
Expression rewritten = Coercer.addCoercions(expression, analysis);
rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), rewritten);
values.add(castToRowExpression(rewritten));
VariableReferenceExpression input = variableAllocator.newVariable(rewritten, type);
argumentVariables.add(new VariableReferenceExpression(getSourceLocation(rewritten), input.getName(), type));
if (type instanceof ArrayType) {
Type elementType = ((ArrayType) type).getElementType();
if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
ImmutableList.Builder<VariableReferenceExpression> unnestVariableBuilder = ImmutableList.builder();
for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
unnestVariableBuilder.add(unnestedVariablesIterator.next());
}
unnestVariables.put(input, unnestVariableBuilder.build());
} else {
unnestVariables.put(input, ImmutableList.of(unnestedVariablesIterator.next()));
}
} else if (type instanceof MapType) {
unnestVariables.put(input, ImmutableList.of(unnestedVariablesIterator.next(), unnestedVariablesIterator.next()));
} else {
throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
}
}
Optional<VariableReferenceExpression> ordinalityVariable = node.isWithOrdinality() ? Optional.of(unnestedVariablesIterator.next()) : Optional.empty();
checkState(!unnestedVariablesIterator.hasNext(), "Not all output variables were matched with input variables");
ValuesNode valuesNode = new ValuesNode(getSourceLocation(node), idAllocator.getNextId(), argumentVariables.build(), ImmutableList.of(values.build()));
UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), valuesNode, ImmutableList.of(), unnestVariables.build(), ordinalityVariable);
return new RelationPlan(unnestNode, scope, unnestedVariables);
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestStreamSummary method getMapFromStreamSummaryOfSlices.
private Map<Slice, Long> getMapFromStreamSummaryOfSlices(StreamSummary streamSummary) {
MapType mapType = mapType(VARCHAR, BIGINT);
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
streamSummary.topK(blockBuilder);
Block object = mapType.getObject(blockBuilder, 0);
Map<Slice, Long> buckets = getMapFromSliceBucket(object);
return buckets;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class TestStreamSummary method getMapForLongType.
private Map<Long, Long> getMapForLongType(StreamSummary histogram) {
MapType mapType = mapType(BIGINT, BIGINT);
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
histogram.topK(blockBuilder);
Block object = mapType.getObject(blockBuilder, 0);
Map<Long, Long> buckets = getMapFromLongBucket(object);
return buckets;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class OrcStorageManager method toOrcFileType.
static Type toOrcFileType(Type raptorType, TypeManager typeManager) {
// TIMESTAMPS are stored as BIGINT to void the poor encoding in ORC
if (raptorType == TimestampType.TIMESTAMP) {
return BIGINT;
}
if (raptorType instanceof ArrayType) {
Type elementType = toOrcFileType(((ArrayType) raptorType).getElementType(), typeManager);
return new ArrayType(elementType);
}
if (raptorType instanceof MapType) {
TypeSignature keyType = toOrcFileType(((MapType) raptorType).getKeyType(), typeManager).getTypeSignature();
TypeSignature valueType = toOrcFileType(((MapType) raptorType).getValueType(), typeManager).getTypeSignature();
return typeManager.getParameterizedType(StandardTypes.MAP, ImmutableList.of(TypeSignatureParameter.of(keyType), TypeSignatureParameter.of(valueType)));
}
if (raptorType instanceof RowType) {
List<RowType.Field> fields = ((RowType) raptorType).getFields().stream().map(field -> new RowType.Field(field.getName(), toOrcFileType(field.getType(), typeManager))).collect(toImmutableList());
return RowType.from(fields);
}
return raptorType;
}
use of com.facebook.presto.common.type.MapType in project presto by prestodb.
the class StructuralTestUtil method mapBlockOf.
public static Block mapBlockOf(Type keyType, Type valueType, Object key, Object value) {
MapType mapType = mapType(keyType, valueType);
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, 10);
BlockBuilder singleMapBlockWriter = blockBuilder.beginBlockEntry();
appendToBlockBuilder(keyType, key, singleMapBlockWriter);
appendToBlockBuilder(valueType, value, singleMapBlockWriter);
blockBuilder.closeEntry();
return mapType.getObject(blockBuilder, 0);
}
Aggregations