mosche commented on a change in pull request #16947:
URL: https://github.com/apache/beam/pull/16947#discussion_r833602836



##########
File path: 
sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/values/AwsPojoRow.java
##########
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.values;
+
+import static java.util.stream.Collectors.toList;
+import static java.util.stream.Collectors.toMap;
+import static 
org.apache.beam.sdk.io.aws2.schemas.AwsPojoSchema.CACHE_IDX_OPTION;
+import static 
org.apache.beam.sdk.io.aws2.schemas.AwsPojoSchema.CACHE_SIZE_OPTION;
+import static software.amazon.awssdk.core.protocol.MarshallingType.LIST;
+import static software.amazon.awssdk.core.protocol.MarshallingType.MAP;
+import static software.amazon.awssdk.core.protocol.MarshallingType.SDK_POJO;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.function.Function;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.Schema.Field;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.commons.lang3.ArrayUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import software.amazon.awssdk.core.SdkField;
+import software.amazon.awssdk.core.SdkPojo;
+import software.amazon.awssdk.core.protocol.MarshallingType;
+import software.amazon.awssdk.core.traits.ListTrait;
+import software.amazon.awssdk.core.traits.MapTrait;
+
+/**
+ * A row delegating to an underlying AWS {@link SdkPojo}.
+ *
+ * <p>This is following the example of {@link RowWithGetters}, but optimized 
for usage with {@link
+ * SdkPojo}s.

Review comment:
       Thanks so much for looking into this @TheNeuralBit 🙏 
   
   For completeness, I wasn't able to use the existing `JavaFieldSchema` or 
`JavaBeanSchema` for the following reasons:
   - Model fields of the AWS SDK are private (and final).
   - Getters / Setters lack expected prefixes to be valid beans.
   - (Immutable) instances must be created using a builder (but these obviously 
lack the expected annotations).
   - Some SDK models are self recursive, these are a major annoyance and 
required logic to eventually skip nested recursive fields as Beam schemas don't 
support this.
   
   My initial version was implemented using `RowWithGetters`, but it required a 
couple of work arounds and seemed sub-optimal:
   
   - I faced a mismatch when using logical types and I'm more and more 
wondering if that's a bug: `RowWithGetters` expects the getter to return the 
base type, but the target object will contain the input type (unless I 
misunderstand) forcing a pointless conversion input type -> base type -> input 
type ([see 
here](https://github.com/apache/beam/pull/11074#discussion_r818731944)). 
Particularly when working with collection types that seemed very inefficient.
   
   - The getter factory `Factory<List<FieldValueGetter>>` doesn't work well 
with AWS model classes. Model instances already contain the necessary metadata 
(`List<SdkField<?>> sdkFields()`) including getters, making traversal of 
[nested models / 
rows](https://github.com/mosche/beam/blob/BEAM-13416-AwsSchemas/sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/values/AwsPojoRow.java#L117-L123)
 trivial. Using the getter factory, that field metadata isn't easily accessible 
as [factories operate on the class & 
schema](https://github.com/apache/beam/blob/master/sdks/java/core/src/main/java/org/apache/beam/sdk/values/RowWithGetters.java#L66).
 
   
   Additionally, there's a couple of optimisations. `RowWithGetters` might also 
benefit from these:
   
   - [Nested 
rows](https://github.com/apache/beam/blob/master/sdks/java/core/src/main/java/org/apache/beam/sdk/values/RowWithGetters.java#L111)
 should be cached, otherwise the cache of such nested rows can't be leveraged
   
   - Handling of collections / maps / iterables can be significantly improved 
in all cases where `getValue` is the [identity 
transform](https://github.com/apache/beam/blob/master/sdks/java/core/src/main/java/org/apache/beam/sdk/values/RowWithGetters.java#L142).
 In such cases the original collection / map / iterable can be returned as is 
(also making caching obsolete in that case).
   
   - Caching using [3 separate hash 
maps](https://github.com/apache/beam/blob/master/sdks/java/core/src/main/java/org/apache/beam/sdk/values/RowWithGetters.java#L57-L59)
 comes with very high overhead for small rows (3 * `initialCapacity`). I 
figured I could use a single `Object[]` for caching as it's possible to 
[precalculate the required cache size and 
indices](https://github.com/mosche/beam/blob/BEAM-13416-AwsSchemas/sdks/java/io/amazon-web-services2/src/main/java/org/apache/beam/sdk/io/aws2/schemas/AwsPojoSchema.java#L242-L245)
 and store this using schema & field options.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to