use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class StructuralTestUtil method rowBlockOf.
public static Block rowBlockOf(List<Type> parameterTypes, Object... values) {
RowType rowType = RowType.anonymous(parameterTypes);
BlockBuilder blockBuilder = rowType.createBlockBuilder(null, 1);
BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < values.length; i++) {
appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
}
blockBuilder.closeEntry();
return rowType.getObject(blockBuilder, 0);
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class AbstractTestHiveClient method assertValueTypes.
private static void assertValueTypes(MaterializedRow row, List<ColumnMetadata> schema) {
for (int columnIndex = 0; columnIndex < schema.size(); columnIndex++) {
ColumnMetadata column = schema.get(columnIndex);
Object value = row.getField(columnIndex);
if (value != null) {
if (BOOLEAN.equals(column.getType())) {
assertInstanceOf(value, Boolean.class);
} else if (TINYINT.equals(column.getType())) {
assertInstanceOf(value, Byte.class);
} else if (SMALLINT.equals(column.getType())) {
assertInstanceOf(value, Short.class);
} else if (INTEGER.equals(column.getType())) {
assertInstanceOf(value, Integer.class);
} else if (BIGINT.equals(column.getType())) {
assertInstanceOf(value, Long.class);
} else if (DOUBLE.equals(column.getType())) {
assertInstanceOf(value, Double.class);
} else if (REAL.equals(column.getType())) {
assertInstanceOf(value, Float.class);
} else if (isVarcharType(column.getType())) {
assertInstanceOf(value, String.class);
} else if (isCharType(column.getType())) {
assertInstanceOf(value, String.class);
} else if (VARBINARY.equals(column.getType())) {
assertInstanceOf(value, SqlVarbinary.class);
} else if (TIMESTAMP.equals(column.getType())) {
assertInstanceOf(value, SqlTimestamp.class);
} else if (DATE.equals(column.getType())) {
assertInstanceOf(value, SqlDate.class);
} else if (column.getType() instanceof ArrayType || column.getType() instanceof RowType) {
assertInstanceOf(value, List.class);
} else if (column.getType() instanceof MapType) {
assertInstanceOf(value, Map.class);
} else {
fail("Unknown primitive type " + columnIndex);
}
}
}
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class BigQueryResultPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (type instanceof ArrayType && value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
appendTo(type.getTypeParameters().get(0), element, builder);
}
output.closeEntry();
return;
}
if (type instanceof RowType && value instanceof GenericRecord) {
GenericRecord record = (GenericRecord) value;
BlockBuilder builder = output.beginBlockEntry();
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
for (int index = 0; index < type.getTypeParameters().size(); index++) {
appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
}
throw new BigQueryException(BIGQUERY_UNSUPPORTED_TYPE_FOR_BLOCK, "Unhandled type for Block: " + type.getTypeSignature());
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class TestTypeConversions method testConvertTwoLevelsRecordField.
@Test
public void testConvertTwoLevelsRecordField() {
Field field = Field.of("rec", RECORD, Field.of("sub_rec", RECORD, Field.of("sub_sub_s", STRING), Field.of("sub_sub_i", INTEGER)), Field.of("sub_s", STRING), Field.of("sub_i", INTEGER));
ColumnMetadata metadata = Conversions.toColumnMetadata(field);
RowType targetType = RowType.from(ImmutableList.of(RowType.field("sub_rec", RowType.from(ImmutableList.of(RowType.field("sub_sub_s", VarcharType.VARCHAR), RowType.field("sub_sub_i", BigintType.BIGINT)))), RowType.field("sub_s", VarcharType.VARCHAR), RowType.field("sub_i", BigintType.BIGINT)));
assertThat(metadata.getType()).isEqualTo(targetType);
}
use of com.facebook.presto.common.type.RowType in project presto by prestodb.
the class RelationPlanner method planCrossJoinUnnest.
private RelationPlan planCrossJoinUnnest(RelationPlan leftPlan, Join joinNode, Unnest node) {
RelationType unnestOutputDescriptor = analysis.getOutputDescriptor(node);
// Create variables for the result of unnesting
ImmutableList.Builder<VariableReferenceExpression> unnestedVariablesBuilder = ImmutableList.builder();
for (Field field : unnestOutputDescriptor.getVisibleFields()) {
VariableReferenceExpression variable = variableAllocator.newVariable(field);
unnestedVariablesBuilder.add(variable);
}
ImmutableList<VariableReferenceExpression> unnestedVariables = unnestedVariablesBuilder.build();
// Add a projection for all the unnest arguments
PlanBuilder planBuilder = initializePlanBuilder(leftPlan);
planBuilder = planBuilder.appendProjections(node.getExpressions(), variableAllocator, idAllocator);
TranslationMap translations = planBuilder.getTranslations();
ProjectNode projectNode = (ProjectNode) planBuilder.getRoot();
ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
UnmodifiableIterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
for (Expression expression : node.getExpressions()) {
Type type = analysis.getType(expression);
VariableReferenceExpression inputVariable = new VariableReferenceExpression(getSourceLocation(expression), translations.get(expression).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(inputVariable, unnestVariableBuilder.build());
} else {
unnestVariables.put(inputVariable, ImmutableList.of(unnestedVariablesIterator.next()));
}
} else if (type instanceof MapType) {
unnestVariables.put(inputVariable, 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");
UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), projectNode, leftPlan.getFieldMappings(), unnestVariables.build(), ordinalityVariable);
return new RelationPlan(unnestNode, analysis.getScope(joinNode), unnestNode.getOutputVariables());
}
Aggregations