Search in sources :

Example 1 with FieldDescriptor

use of org.apache.beam.sdk.schemas.FieldAccessDescriptor.FieldDescriptor in project beam by apache.

the class SelectHelpers method selectIntoRow.

/**
 * Select out of a given {@link Row} object.
 */
private static void selectIntoRow(Schema inputSchema, Row input, Row.Builder output, FieldAccessDescriptor fieldAccessDescriptor) {
    if (fieldAccessDescriptor.getAllFields()) {
        List<Object> values = (input != null) ? input.getValues() : Collections.nCopies(inputSchema.getFieldCount(), null);
        output.addValues(values);
        return;
    }
    for (int fieldId : fieldAccessDescriptor.fieldIdsAccessed()) {
        // TODO: Once we support specific qualifiers (like array slices), extract them here.
        output.addValue((input != null) ? input.getValue(fieldId) : null);
    }
    Schema outputSchema = output.getSchema();
    for (Map.Entry<FieldDescriptor, FieldAccessDescriptor> nested : fieldAccessDescriptor.getNestedFieldsAccessed().entrySet()) {
        FieldDescriptor field = nested.getKey();
        FieldAccessDescriptor nestedAccess = nested.getValue();
        FieldType nestedInputType = inputSchema.getField(field.getFieldId()).getType();
        FieldType nestedOutputType = outputSchema.getField(output.nextFieldId()).getType();
        selectIntoRowWithQualifiers(field.getQualifiers(), 0, input.getValue(field.getFieldId()), output, nestedAccess, nestedInputType, nestedOutputType);
    }
}
Also used : FieldAccessDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor) Schema(org.apache.beam.sdk.schemas.Schema) Map(java.util.Map) FieldDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor.FieldDescriptor) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType)

Example 2 with FieldDescriptor

use of org.apache.beam.sdk.schemas.FieldAccessDescriptor.FieldDescriptor in project beam by apache.

the class SelectHelpers method getOutputSchemaTrackingNullable.

private static Schema getOutputSchemaTrackingNullable(Schema inputSchema, FieldAccessDescriptor fieldAccessDescriptor, boolean isNullable) {
    if (fieldAccessDescriptor.getAllFields()) {
        Schema schemaToReturn = inputSchema;
        if (isNullable) {
            // Some parent field in the selector was nullable, so we must mark all of these fields
            // nullable.
            schemaToReturn = inputSchema.getFields().stream().map(f -> f.withNullable(true)).collect(Schema.toSchema());
        }
        return schemaToReturn;
    }
    List<Schema> schemas = Lists.newArrayList();
    Schema.Builder builder = Schema.builder();
    for (FieldDescriptor fieldDescriptor : fieldAccessDescriptor.getFieldsAccessed()) {
        Field field = inputSchema.getField(fieldDescriptor.getFieldId());
        if (fieldDescriptor.getFieldRename() != null) {
            field = field.withName(fieldDescriptor.getFieldRename());
        }
        // be nullable if a is nullable (even if b was not in the original schema).
        if (isNullable) {
            field = field.withNullable(true);
        }
        builder.addField(field);
    }
    schemas.add(builder.build());
    for (Map.Entry<FieldDescriptor, FieldAccessDescriptor> nested : fieldAccessDescriptor.getNestedFieldsAccessed().entrySet()) {
        FieldDescriptor fieldDescriptor = nested.getKey();
        FieldAccessDescriptor nestedAccess = nested.getValue();
        Field field = inputSchema.getField(checkNotNull(fieldDescriptor.getFieldId()));
        if (fieldDescriptor.getFieldRename() != null) {
            field = field.withName(fieldDescriptor.getFieldRename());
        }
        Schema outputSchema = getOutputSchemaHelper(field.getType(), nestedAccess, fieldDescriptor.getQualifiers(), 0, isNullable || field.getType().getNullable());
        schemas.add(outputSchema);
    }
    return union(schemas);
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) FieldAccessDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor) Schema(org.apache.beam.sdk.schemas.Schema) Map(java.util.Map) FieldDescriptor(org.apache.beam.sdk.schemas.FieldAccessDescriptor.FieldDescriptor)

Aggregations

Map (java.util.Map)2 FieldAccessDescriptor (org.apache.beam.sdk.schemas.FieldAccessDescriptor)2 FieldDescriptor (org.apache.beam.sdk.schemas.FieldAccessDescriptor.FieldDescriptor)2 Schema (org.apache.beam.sdk.schemas.Schema)2 Field (org.apache.beam.sdk.schemas.Schema.Field)1 FieldType (org.apache.beam.sdk.schemas.Schema.FieldType)1