-
Notifications
You must be signed in to change notification settings - Fork 4.5k
[BEAM-7996] Add support for MapType and Nulls in container types for Python RowCoder #12426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
TheNeuralBit
merged 10 commits into
apache:master
from
TheNeuralBit:row-map-and-container-nulls
Aug 6, 2020
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
e850957
Add support for encoding Maps and Nulls (in container types) in Pytho…
TheNeuralBit 587dde5
fixup! Add support for encoding Maps and Nulls (in container types) i…
TheNeuralBit 0dfff97
fixup! Add support for encoding Maps and Nulls (in container types) i…
TheNeuralBit 16c06ea
Don't specify nested in row coder tests
TheNeuralBit 3335440
fixup! Add support for encoding Maps and Nulls (in container types) i…
TheNeuralBit 0416e2e
Don't mutate value when reading rows
TheNeuralBit 9bb591d
fixup! Add support for encoding Maps and Nulls (in container types) i…
TheNeuralBit d4f5598
fixup! Add support for encoding Maps and Nulls (in container types) i…
TheNeuralBit 73f5602
Python: Don't mutate value
TheNeuralBit 31f246e
fixup! Add support for encoding Maps and Nulls (in container types) i…
TheNeuralBit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ | |
| import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.MoreObjects.firstNonNull; | ||
| import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull; | ||
| import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableList.toImmutableList; | ||
| import static org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static org.hamcrest.Matchers.equalTo; | ||
| import static org.hamcrest.Matchers.hasItem; | ||
| import static org.hamcrest.Matchers.instanceOf; | ||
|
|
@@ -43,6 +42,7 @@ | |
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -340,6 +340,10 @@ private static Object convertValue(Object value, CommonCoder coderSpec, Coder co | |
| } | ||
|
|
||
| private static Object parseField(Object value, Schema.FieldType fieldType) { | ||
| if (value == null) { | ||
| return null; | ||
| } | ||
|
|
||
| switch (fieldType.getTypeName()) { | ||
| case BYTE: | ||
| return ((Number) value).byteValue(); | ||
|
|
@@ -366,14 +370,18 @@ private static Object parseField(Object value, Schema.FieldType fieldType) { | |
| .map((element) -> parseField(element, fieldType.getCollectionElementType())) | ||
| .collect(toImmutableList()); | ||
| case MAP: | ||
| Map<Object, Object> kvMap = (Map<Object, Object>) value; | ||
| return kvMap.entrySet().stream() | ||
| .collect( | ||
| toImmutableMap( | ||
| (pair) -> parseField(pair.getKey(), fieldType.getMapKeyType()), | ||
| (pair) -> parseField(pair.getValue(), fieldType.getMapValueType()))); | ||
| Map<Object, Object> kvMap = new HashMap<>(); | ||
| ((Map<Object, Object>) value) | ||
| .entrySet().stream() | ||
| .forEach( | ||
| (entry) -> | ||
| kvMap.put( | ||
| parseField(entry.getKey(), fieldType.getMapKeyType()), | ||
| parseField(entry.getValue(), fieldType.getMapValueType()))); | ||
| return kvMap; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is necessary because |
||
| case ROW: | ||
| Map<String, Object> rowMap = (Map<String, Object>) value; | ||
| // Clone map so we don't mutate the underlying value | ||
| Map<String, Object> rowMap = new HashMap<>((Map<String, Object>) value); | ||
| Schema schema = fieldType.getRowSchema(); | ||
| Row.Builder row = Row.withSchema(schema); | ||
| for (Schema.Field field : schema.getFields()) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI @lostluck - previously the encoding for MapType and for nullable types within containers was not documented.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack! Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this just the encoding of a nullable type? (Why does it have to be called out specially?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For maps specifically, do we want to allow null keys? Is it valuable to have null values (as distinct from just not present)? I might lean towards disallowing nulls and then possibly allowing it in the future if we have good reason to, which will be forward compatible.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is specifically for nullable types that are elements of an Array or keys/values of a Map. For rows we encode nulls in a separate bitmask:
beam/model/pipeline/src/main/proto/beam_runner_api.proto
Lines 837 to 843 in 587dde5
Java schemas will already let you use nullable types for keys and values in Maps. I doubt anyone is relying on it.. but there's a risk if we disallow it now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the verdict here is to ignore the nullability field of a FieldType when it's nested in a Map or Array?
That's... unfortunate. Must we bend over backwards to maintain compatibility with Java's previous encoding, which wasn't officially a schema encoding until this PR? It seems very strange that we're already relying on unspecified behaviour.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, this requirement means that all values must be pointers (or reference types) in Go as ordinary primitives cannot be nullable. That feels very strange.
EDIT: Please disregard my last comments, I misread that this only applies to when the field is specified as nullable.
I misunderstood that the discussion is orthogonal to that (whether to allow nullable map components at all.)
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@robertwb Your comment fed my own misunderstanding. It is possible to declare a map in schemas as not having null/keys values, but not necessarily on the SDK side.
Technically, there's no reason that the SDK can't use a non-nullable containing version of the container if the Key and Value components are not themselves marked as nillable. IIRC, the Java SDK could converted ImmutableMaps or similar into just non-nullable Key and non-nullable Value types.
The issue as I'm understanding it is that the limitation is on the SDK Language side, rather than the schema specification side, as discussed the schemas fields can individually have their nullable bits set.
Eg. Go doesn't have this ambiguity for map types.
On the other hand, in Go, Iterable/array types which will be represented by slices will have this ambiguity when used as a field, as they can be nil, and could also still be pointers to said reference types. That ambiguity is well known enough that pointers to reference types (maps, slices, chans..) are strongly discouraged in idiomatic Go.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@lostluck The issue here is what kinds of objects the SDK must accept when receiving these from a foreign SDK. If nulls are allowed, you cannot let the element type be (say) a
map[str, int]in go.It looks like there is a way to specify in the schema whether the keys/values could be null (or explicitly disallow it for either or both). The encoding, on the other hand, always has this leading bit set (regardless of whether the schema allows null values), but is harder to change for backwards compatibility reasons. If this is the case, I'm OK with that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, we set this prefix depending on the bit in the schema. All is good.