You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@beam.apache.org by GitBox <gi...@apache.org> on 2020/03/11 06:20:50 UTC

[GitHub] [beam] reuvenlax commented on a change in pull request #11074: Store logical type values in Row instead of base values

reuvenlax commented on a change in pull request #11074: Store logical type values in Row instead of base values
URL: https://github.com/apache/beam/pull/11074#discussion_r390764992
 
 

 ##########
 File path: sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/SchemaCoderHelpers.java
 ##########
 @@ -0,0 +1,132 @@
+/*
+ * 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.schemas;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import org.apache.beam.sdk.coders.BigDecimalCoder;
+import org.apache.beam.sdk.coders.BigEndianShortCoder;
+import org.apache.beam.sdk.coders.BooleanCoder;
+import org.apache.beam.sdk.coders.ByteArrayCoder;
+import org.apache.beam.sdk.coders.ByteCoder;
+import org.apache.beam.sdk.coders.Coder;
+import org.apache.beam.sdk.coders.CoderException;
+import org.apache.beam.sdk.coders.DoubleCoder;
+import org.apache.beam.sdk.coders.FloatCoder;
+import org.apache.beam.sdk.coders.InstantCoder;
+import org.apache.beam.sdk.coders.IterableCoder;
+import org.apache.beam.sdk.coders.ListCoder;
+import org.apache.beam.sdk.coders.MapCoder;
+import org.apache.beam.sdk.coders.NullableCoder;
+import org.apache.beam.sdk.coders.StringUtf8Coder;
+import org.apache.beam.sdk.coders.VarIntCoder;
+import org.apache.beam.sdk.coders.VarLongCoder;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.beam.sdk.schemas.Schema.LogicalType;
+import org.apache.beam.sdk.schemas.Schema.TypeName;
+import org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap;
+
+public class SchemaCoderHelpers {
+  // This contains a map of primitive types to their coders.
+  public static final Map<TypeName, Coder> CODER_MAP =
+      ImmutableMap.<TypeName, Coder>builder()
+          .put(TypeName.BYTE, ByteCoder.of())
+          .put(TypeName.BYTES, ByteArrayCoder.of())
+          .put(TypeName.INT16, BigEndianShortCoder.of())
+          .put(TypeName.INT32, VarIntCoder.of())
+          .put(TypeName.INT64, VarLongCoder.of())
+          .put(TypeName.DECIMAL, BigDecimalCoder.of())
+          .put(TypeName.FLOAT, FloatCoder.of())
+          .put(TypeName.DOUBLE, DoubleCoder.of())
+          .put(TypeName.STRING, StringUtf8Coder.of())
+          .put(TypeName.DATETIME, InstantCoder.of())
+          .put(TypeName.BOOLEAN, BooleanCoder.of())
+          .build();
+
+  private static class LogicalTypeCoder extends Coder {
+    private final LogicalType logicalType;
+    private final Coder baseTypeCoder;
+
+    public LogicalTypeCoder(LogicalType logicalType, Coder baseTypeCoder) {
+      this.logicalType = logicalType;
+      this.baseTypeCoder = baseTypeCoder;
+    }
+
+    @Override
+    public void encode(Object value, OutputStream outStream) throws CoderException, IOException {
+      Object baseType = logicalType.toBaseType(value);
+      baseTypeCoder.encode(baseType, outStream);
+    }
+
+    @Override
+    public Object decode(InputStream inStream) throws CoderException, IOException {
+      Object baseType = baseTypeCoder.decode(inStream);
+      return logicalType.toInputType(baseType);
+    }
+
+    @Override
+    public List<? extends Coder<?>> getCoderArguments() {
+      return Collections.emptyList();
+    }
+
+    @Override
+    public void verifyDeterministic() throws NonDeterministicException {
+      baseTypeCoder.verifyDeterministic();
+    }
+  }
+
+  /** Returns the coder used for a given primitive type. */
+  public static <T> Coder<T> coderForFieldType(FieldType fieldType) {
+    Coder<T> coder;
+    switch (fieldType.getTypeName()) {
+      case ROW:
+        coder = (Coder<T>) SchemaCoder.of(fieldType.getRowSchema());
+        break;
+      case ARRAY:
+        coder = (Coder<T>) ListCoder.of(coderForFieldType(fieldType.getCollectionElementType()));
+        break;
+      case ITERABLE:
+        coder =
+            (Coder<T>) IterableCoder.of(coderForFieldType(fieldType.getCollectionElementType()));
+        break;
+      case MAP:
+        coder =
+            (Coder<T>)
+                MapCoder.of(
+                    coderForFieldType(fieldType.getMapKeyType()),
+                    coderForFieldType(fieldType.getMapValueType()));
+        break;
+      case LOGICAL_TYPE:
+        coder =
+            new LogicalTypeCoder(
+                fieldType.getLogicalType(),
+                coderForFieldType(fieldType.getLogicalType().getBaseType()));
+        break;
+      default:
+        coder = (Coder<T>) CODER_MAP.get(fieldType.getTypeName());
 
 Review comment:
   Added  a null check.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services