Github user zentol commented on a diff in the pull request:

    https://github.com/apache/flink/pull/3501#discussion_r105239799
  
    --- Diff: 
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/datastream/KeyedStream.java
 ---
    @@ -114,9 +121,53 @@ public KeyedStream(DataStream<T> dataStream, 
KeySelector<T, KEY> keySelector, Ty
                                dataStream.getTransformation(),
                                new KeyGroupStreamPartitioner<>(keySelector, 
StreamGraphGenerator.DEFAULT_LOWER_BOUND_MAX_PARALLELISM)));
                this.keySelector = keySelector;
    -           this.keyType = keyType;
    +           this.keyType = validateKeyType(keyType);
        }
    -   
    +
    +   private TypeInformation<KEY> validateKeyType(TypeInformation<KEY> 
keyType) {
    +           Stack<TypeInformation<?>> stack = new Stack<>();
    +           stack.push(keyType);
    +
    +           while (!stack.isEmpty()) {
    +                   TypeInformation<?> typeInfo = stack.pop();
    +
    +                   if (!validateKeyTypeIsHashable(typeInfo)) {
    +                           throw new InvalidProgramException("This type (" 
+ keyType + ") cannot be used as key.");
    +                   }
    +                   
    +                   if (typeInfo instanceof TupleTypeInfoBase) {
    +                           for (int i = 0; i < typeInfo.getArity(); i++) {
    +                                   stack.push(((TupleTypeInfoBase) 
typeInfo).getTypeAt(i));        
    +                           }
    +                   }
    +           }
    +           return keyType;
    +   }
    +
    +   /**
    +    * Validates that a given type of element (as encoded by the provided 
{@link TypeInformation}) can be
    +    * used as a key in the {@code DataStream.keyBy()} operation.
    +    *
    +    * @return {@code false} if:
    +    * <ol>
    +    *     <li>it is a POJO type but does not override the {@link 
#hashCode()} method and relies on
    +    *     the {@link Object#hashCode()} implementation.</li>
    +    *     <li>it is an array of any type (see {@link 
PrimitiveArrayTypeInfo}, {@link BasicArrayTypeInfo},
    +    *     {@link ObjectArrayTypeInfo}).</li>
    +    * </ol>,
    +    * {@code true} otherwise.
    +    */
    +   private boolean validateKeyTypeIsHashable(TypeInformation<?> type) {
    +           try {
    +                   return (type instanceof PojoTypeInfo) ?
    --- End diff --
    
    ? and : are typically at the start of a new line.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to