Github user zentol commented on a diff in the pull request:
https://github.com/apache/flink/pull/3501#discussion_r105405543
--- 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 --
So i looked through the code base, and found 5 different patterns, so
you're right, "typical" doesn't really fit:
1:
```
<condition> ? <then> : <else>
```
2:
```
<condition> ?
<then> : <else>
```
3:
```
<condition> ?
<then>
: <else>
```
4:
```
<condition>
? <then>
: <else>
```
5:
```
if <condition> ?
<then> :
<else>
```
In my opinion, only 1 and 4 should be used; and 1 only for short
statements. Both of these options are symmetrical; the if and else statement
are formatted the same way, and place important information (? and \:) at the
start of the line.
The point here is pattern recognition.
When seeing this
```
X
? Y
: Z
```
you can immediately see what is going on without looking at the details of
the expression, nor scanning any line; the very first character gives it away.
The position of ? and \: is also static, it's on the next line with an
additional indent, compared to other options which may hide ? at the end of a
100 character expression (which gets especially funky with option 2 which also
hides \: somewhere, so at a glace it could also just be a method call with many
arguments).
---
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 [email protected] or file a JIRA ticket
with INFRA.
---