use of com.facebook.presto.common.type.RowType 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.RowType in project presto by prestodb.
the class TestWriterBlockRawSize method testRowType.
@Test
public void testRowType() {
Type elementType = INTEGER;
Type rowType = RowType.anonymous(ImmutableList.of(elementType, elementType));
ColumnWriter columnWriter = createColumnWriter(rowType);
BlockBuilder blockBuilder = elementType.createBlockBuilder(null, NUM_ELEMENTS);
boolean[] isNull = new boolean[NUM_ELEMENTS * 2];
for (int i = 0; i < NUM_ELEMENTS; i++) {
elementType.writeLong(blockBuilder, i);
isNull[i * 2] = true;
}
Block elementBlock = blockBuilder.build();
Block[] elementBlocks = new Block[] { elementBlock, elementBlock };
Block rowBlock = RowBlock.fromFieldBlocks(NUM_ELEMENTS * 2, Optional.of(isNull), elementBlocks);
long rawSize = columnWriter.writeBlock(rowBlock);
long expectedSize = NUM_ELEMENTS + (NUM_ELEMENTS * 2 * ((FixedWidthType) elementType).getFixedSize());
assertEquals(rawSize, expectedSize);
}
use of com.facebook.presto.common.type.RowType 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.RowType in project presto by prestodb.
the class H2QueryRunner method mapRowValues.
private static Object[] mapRowValues(RowType rowType, Object[] values) {
int fieldCount = rowType.getFields().size();
Object[] fields = new Object[fieldCount];
for (int j = 0; j < fieldCount; j++) {
Type fieldType = rowType.getTypeParameters().get(j);
if (fieldType instanceof RowType) {
fields[j] = newArrayList(mapRowValues((RowType) fieldType, (Object[]) values[j]));
} else {
fields[j] = values[j];
}
}
return fields;
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class QueryRewriter method getColumnTypeRewrite.
private Optional<Type> getColumnTypeRewrite(Type type) {
if (type.equals(DATE) || type.equals(TIME)) {
return Optional.of(TIMESTAMP);
}
if (type.equals(TIMESTAMP_WITH_TIME_ZONE)) {
return Optional.of(VARCHAR);
}
if (type.equals(UNKNOWN)) {
return Optional.of(BIGINT);
}
if (type instanceof DecimalType) {
return Optional.of(DOUBLE);
}
if (type instanceof ArrayType) {
return getColumnTypeRewrite(((ArrayType) type).getElementType()).map(ArrayType::new);
}
if (type instanceof MapType) {
Type keyType = ((MapType) type).getKeyType();
Type valueType = ((MapType) type).getValueType();
Optional<Type> keyTypeRewrite = getColumnTypeRewrite(keyType);
Optional<Type> valueTypeRewrite = getColumnTypeRewrite(valueType);
if (keyTypeRewrite.isPresent() || valueTypeRewrite.isPresent()) {
return Optional.of(typeManager.getType(new TypeSignature(MAP, TypeSignatureParameter.of(keyTypeRewrite.orElse(keyType).getTypeSignature()), TypeSignatureParameter.of(valueTypeRewrite.orElse(valueType).getTypeSignature()))));
}
return Optional.empty();
}
if (type instanceof RowType) {
List<Field> fields = ((RowType) type).getFields();
List<Field> fieldsRewrite = new ArrayList<>();
boolean rewrite = false;
for (Field field : fields) {
Optional<Type> fieldTypeRewrite = getColumnTypeRewrite(field.getType());
rewrite = rewrite || fieldTypeRewrite.isPresent();
fieldsRewrite.add(new Field(field.getName(), fieldTypeRewrite.orElse(field.getType())));
}
return rewrite ? Optional.of(RowType.from(fieldsRewrite)) : Optional.empty();
}
return Optional.empty();
}
Aggregations