Java Code Examples for org.apache.beam.sdk.values.TypeDescriptor#getType()

The following examples show how to use org.apache.beam.sdk.values.TypeDescriptor#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 6 votes vote down vote up
private static FieldType getArrayFieldType(TypeDescriptor typeDescriptor) {
  if (typeDescriptor.isArray()) {
    if (typeDescriptor.getComponentType().getType().equals(byte.class)) {
      return FieldType.BYTES;
    } else {
      return FieldType.array(fieldTypeForJavaType(typeDescriptor.getComponentType()));
    }
  }
  if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
    TypeDescriptor<Collection<?>> collection = typeDescriptor.getSupertype(Collection.class);
    if (collection.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) collection.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      checkArgument(params.length == 1);
      return FieldType.array(fieldTypeForJavaType(TypeDescriptor.of(params[0])));
    }
  }
  throw new RuntimeException("Could not determine array parameter type for field.");
}
 
Example 2
Source File: ReflectUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
/** For an array T[] or a subclass of Iterable<T>, return a TypeDescriptor describing T. */
@Nullable
public static TypeDescriptor getIterableComponentType(TypeDescriptor valueType) {
  TypeDescriptor componentType = null;
  if (valueType.isArray()) {
    Type component = valueType.getComponentType().getType();
    if (!component.equals(byte.class)) {
      // Byte arrays are special cased since we have a schema type corresponding to them.
      componentType = TypeDescriptor.of(component);
    }
  } else if (valueType.isSubtypeOf(TypeDescriptor.of(Iterable.class))) {
    TypeDescriptor<Iterable<?>> collection = valueType.getSupertype(Iterable.class);
    if (collection.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) collection.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      checkArgument(params.length == 1);
      componentType = TypeDescriptor.of(params[0]);
    } else {
      throw new RuntimeException("Collection parameter is not parameterized!");
    }
  }
  return componentType;
}
 
Example 3
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 5 votes vote down vote up
private static FieldType getIterableFieldType(TypeDescriptor typeDescriptor) {
  TypeDescriptor<Iterable<?>> iterable = typeDescriptor.getSupertype(Iterable.class);
  if (iterable.getType() instanceof ParameterizedType) {
    ParameterizedType ptype = (ParameterizedType) iterable.getType();
    java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
    checkArgument(params.length == 1);
    return FieldType.iterable(fieldTypeForJavaType(TypeDescriptor.of(params[0])));
  }
  throw new RuntimeException("Could not determine array parameter type for field.");
}
 
Example 4
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 5 votes vote down vote up
private static FieldType getMapFieldType(TypeDescriptor typeDescriptor) {
  TypeDescriptor<Collection<?>> map = typeDescriptor.getSupertype(Map.class);
  if (map.getType() instanceof ParameterizedType) {
    ParameterizedType ptype = (ParameterizedType) map.getType();
    java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
    return FieldType.map(
        fieldTypeForJavaType(TypeDescriptor.of(params[0])),
        fieldTypeForJavaType(TypeDescriptor.of(params[1])));
  }
  throw new RuntimeException("Cound not determine array parameter type for field.");
}
 
Example 5
Source File: ReflectUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
public static TypeDescriptor getMapType(TypeDescriptor valueType, int index) {
  TypeDescriptor mapType = null;
  if (valueType.isSubtypeOf(TypeDescriptor.of(Map.class))) {
    TypeDescriptor<Collection<?>> map = valueType.getSupertype(Map.class);
    if (map.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) map.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      mapType = TypeDescriptor.of(params[index]);
    } else {
      throw new RuntimeException("Map type is not parameterized! " + map);
    }
  }
  return mapType;
}
 
Example 6
Source File: LazyAvroCoder.java    From components with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Coder<T> coderFor(TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)
        throws CannotProvideCoderException {

    Type t = typeDescriptor.getType();
    if (IndexedRecord.class.isAssignableFrom(typeDescriptor.getRawType())) {
        Coder<T> c = LazyAvroCoder.<T> of();
        return c;
    }
    throw new CannotProvideCoderException(String.format("Cannot provide %s because %s is not implement IndexedRecord",
            LazyAvroCoder.class.getSimpleName(), typeDescriptor));
}
 
Example 7
Source File: AvroCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
private void doCheck(String context, TypeDescriptor<?> type, Schema schema) {
  switch (schema.getType()) {
    case ARRAY:
      checkArray(context, type, schema);
      break;
    case ENUM:
      // Enums should be deterministic, since they depend only on the ordinal.
      break;
    case FIXED:
      // Depending on the implementation of GenericFixed, we don't know how
      // the given field will be encoded. So, we assume that it isn't
      // deterministic.
      reportError(context, "FIXED encodings are not guaranteed to be deterministic");
      break;
    case MAP:
      checkMap(context, type, schema);
      break;
    case RECORD:
      if (!(type.getType() instanceof Class)) {
        reportError(context, "Cannot determine type from generic %s due to erasure", type);
        return;
      }
      checkRecord(type, schema);
      break;
    case UNION:
      checkUnion(context, type, schema);
      break;
    case STRING:
      checkString(context, type);
      break;
    case BOOLEAN:
    case BYTES:
    case DOUBLE:
    case INT:
    case FLOAT:
    case LONG:
    case NULL:
      // For types that Avro encodes using one of the above primitives, we assume they are
      // deterministic.
      break;
    default:
      // In any other case (eg., new types added to Avro) we cautiously return
      // false.
      reportError(context, "Unknown schema type %s may be non-deterministic", schema.getType());
      break;
  }
}
 
Example 8
Source File: CoderRegistry.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code true} if the given {@link Coder} can possibly encode elements of the given type.
 */
@VisibleForTesting
static <T, CoderT extends Coder<T>, CandidateT> void verifyCompatible(
    CoderT coder, Type candidateType) throws IncompatibleCoderException {

  // Various representations of the coder's class
  @SuppressWarnings("unchecked")
  Class<CoderT> coderClass = (Class<CoderT>) coder.getClass();
  TypeDescriptor<CoderT> coderDescriptor = TypeDescriptor.of(coderClass);

  // Various representations of the actual coded type
  @SuppressWarnings("unchecked")
  TypeDescriptor<T> codedDescriptor = CoderUtils.getCodedType(coderDescriptor);
  @SuppressWarnings("unchecked")
  Class<T> codedClass = (Class<T>) codedDescriptor.getRawType();
  Type codedType = codedDescriptor.getType();

  // Various representations of the candidate type
  @SuppressWarnings("unchecked")
  TypeDescriptor<CandidateT> candidateDescriptor =
      (TypeDescriptor<CandidateT>) TypeDescriptor.of(candidateType);
  @SuppressWarnings("unchecked")
  Class<CandidateT> candidateClass = (Class<CandidateT>) candidateDescriptor.getRawType();

  // If coder has type Coder<T> where the actual value of T is lost
  // to erasure, then we cannot rule it out.
  if (candidateType instanceof TypeVariable) {
    return;
  }

  // If the raw types are not compatible, we can certainly rule out
  // coder compatibility
  if (!codedClass.isAssignableFrom(candidateClass)) {
    throw new IncompatibleCoderException(
        String.format(
            "Cannot encode elements of type %s with coder %s because the"
                + " coded type %s is not assignable from %s",
            candidateType, coder, codedClass, candidateType),
        coder,
        candidateType);
  }
  // we have established that this is a covariant upcast... though
  // coders are invariant, we are just checking one direction
  @SuppressWarnings("unchecked")
  TypeDescriptor<T> candidateOkDescriptor = (TypeDescriptor<T>) candidateDescriptor;

  // If the coded type is a parameterized type where any of the actual
  // type parameters are not compatible, then the whole thing is certainly not
  // compatible.
  if ((codedType instanceof ParameterizedType) && !isNullOrEmpty(coder.getCoderArguments())) {
    ParameterizedType parameterizedSupertype =
        (ParameterizedType) candidateOkDescriptor.getSupertype(codedClass).getType();
    Type[] typeArguments = parameterizedSupertype.getActualTypeArguments();
    List<? extends Coder<?>> typeArgumentCoders = coder.getCoderArguments();
    if (typeArguments.length < typeArgumentCoders.size()) {
      throw new IncompatibleCoderException(
          String.format(
              "Cannot encode elements of type %s with coder %s:"
                  + " the generic supertype %s has %s type parameters, which is less than the"
                  + " number of coder arguments %s has (%s).",
              candidateOkDescriptor,
              coder,
              parameterizedSupertype,
              typeArguments.length,
              coder,
              typeArgumentCoders.size()),
          coder,
          candidateOkDescriptor.getType());
    }
    for (int i = 0; i < typeArgumentCoders.size(); i++) {
      try {
        Coder<?> typeArgumentCoder = typeArgumentCoders.get(i);
        verifyCompatible(
            typeArgumentCoder, candidateDescriptor.resolveType(typeArguments[i]).getType());
      } catch (IncompatibleCoderException exn) {
        throw new IncompatibleCoderException(
            String.format(
                "Cannot encode elements of type %s with coder %s"
                    + " because some component coder is incompatible",
                candidateType, coder),
            coder,
            candidateType,
            exn);
      }
    }
  }
}
 
Example 9
Source File: CoderRegistry.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link Coder} to use for values of the given type, in a context where the given types
 * use the given coders.
 *
 * @throws CannotProvideCoderException if a coder cannot be provided
 */
private <T> Coder<T> getCoderFromTypeDescriptor(
    TypeDescriptor<T> typeDescriptor, SetMultimap<Type, Coder<?>> typeCoderBindings)
    throws CannotProvideCoderException {
  Type type = typeDescriptor.getType();
  Coder<?> coder;
  if (typeDescriptor.equals(TypeDescriptors.rows())) {
    throw new CannotProvideCoderException(
        "Cannot provide a coder for a Beam Row. Please provide a schema instead using PCollection.setRowSchema.");
  }
  if (typeCoderBindings.containsKey(type)) {
    Set<Coder<?>> coders = typeCoderBindings.get(type);
    if (coders.size() == 1) {
      coder = Iterables.getOnlyElement(coders);
    } else {
      throw new CannotProvideCoderException(
          String.format(
              "Cannot provide a coder for type variable %s"
                  + " because the actual type is over specified by multiple"
                  + " incompatible coders %s.",
              type, coders),
          ReasonCode.OVER_SPECIFIED);
    }
  } else if (type instanceof Class<?>) {
    coder = getCoderFromFactories(typeDescriptor, Collections.emptyList());
  } else if (type instanceof ParameterizedType) {
    coder = getCoderFromParameterizedType((ParameterizedType) type, typeCoderBindings);
  } else if (type instanceof TypeVariable) {
    coder = getCoderFromFactories(typeDescriptor, Collections.emptyList());
  } else if (type instanceof WildcardType) {
    // No coder for an unknown generic type.
    throw new CannotProvideCoderException(
        String.format("Cannot provide a coder for wildcard type %s.", type), ReasonCode.UNKNOWN);
  } else {
    throw new RuntimeException("Internal error: unexpected kind of Type: " + type);
  }

  LOG.debug("Coder for {}: {}", typeDescriptor, coder);
  @SuppressWarnings("unchecked")
  Coder<T> result = (Coder<T>) coder;
  return result;
}
 
Example 10
Source File: GetterBasedSchemaProvider.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> SerializableFunction<Row, T> fromRowFunction(TypeDescriptor<T> typeDescriptor) {
  Class<T> clazz = (Class<T>) typeDescriptor.getType();
  return new FromRowUsingCreator<>(clazz, this);
}
 
Example 11
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertArray(TypeDescriptor<?> type) {
  TypeDescriptor ret = createCollectionType(type.getComponentType());
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 12
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertCollection(TypeDescriptor<?> type) {
  TypeDescriptor ret = createCollectionType(ReflectUtils.getIterableComponentType(type));
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 13
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertList(TypeDescriptor<?> type) {
  TypeDescriptor ret = createCollectionType(ReflectUtils.getIterableComponentType(type));
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 14
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertIterable(TypeDescriptor<?> type) {
  TypeDescriptor ret = createIterableType(ReflectUtils.getIterableComponentType(type));
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 15
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertDefault(TypeDescriptor<?> type) {
  return returnRawTypes ? type.getRawType() : type.getType();
}