beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "ASF GitHub Bot (JIRA)" <j...@apache.org>
Subject [jira] [Work logged] (BEAM-4461) Create a library of useful transforms that use schemas
Date Sat, 15 Sep 2018 01:07:00 GMT

     [ https://issues.apache.org/jira/browse/BEAM-4461?focusedWorklogId=144520&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-144520
]

ASF GitHub Bot logged work on BEAM-4461:
----------------------------------------

                Author: ASF GitHub Bot
            Created on: 15/Sep/18 01:06
            Start Date: 15/Sep/18 01:06
    Worklog Time Spent: 10m 
      Work Description: reuvenlax closed pull request #6317: [BEAM-4461]  Add mapping between
FieldType and Java types.
URL: https://github.com/apache/beam/pull/6317
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/FieldTypeDescriptors.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/FieldTypeDescriptors.java
new file mode 100644
index 00000000000..252ea5e1831
--- /dev/null
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/schemas/FieldTypeDescriptors.java
@@ -0,0 +1,119 @@
+/*
+ * 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 static com.google.common.base.Preconditions.checkArgument;
+
+import com.google.common.collect.BiMap;
+import com.google.common.collect.ImmutableBiMap;
+import java.lang.reflect.ParameterizedType;
+import java.util.Collection;
+import java.util.Map;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.beam.sdk.schemas.Schema.TypeName;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.joda.time.Instant;
+/**
+ * Utilities for converting between {@link Schema} field types and {@link TypeDescriptor}s
that
+ * define Java objects which can represent these field types.
+ */
+public class FieldTypeDescriptors {
+  private static final BiMap<TypeName, TypeDescriptor> PRIMITIVE_MAPPING =
+      ImmutableBiMap.<TypeName, TypeDescriptor>builder()
+          .put(TypeName.BYTE, TypeDescriptors.bytes())
+          .put(TypeName.INT16, TypeDescriptors.shorts())
+          .put(TypeName.INT32, TypeDescriptors.integers())
+          .put(TypeName.INT64, TypeDescriptors.longs())
+          .put(TypeName.DECIMAL, TypeDescriptors.bigdecimals())
+          .put(TypeName.FLOAT, TypeDescriptors.floats())
+          .put(TypeName.DOUBLE, TypeDescriptors.doubles())
+          .put(TypeName.STRING, TypeDescriptors.strings())
+          .put(TypeName.DATETIME, TypeDescriptor.of(Instant.class))
+          .put(TypeName.BOOLEAN, TypeDescriptors.booleans())
+          .put(TypeName.BYTES, TypeDescriptor.of(byte[].class))
+          .build();
+  /** Get a {@link TypeDescriptor} from a {@link FieldType}. */
+  public static TypeDescriptor javaTypeForFieldType(FieldType fieldType) {
+    switch (fieldType.getTypeName()) {
+      case ARRAY:
+        return TypeDescriptors.lists(javaTypeForFieldType(fieldType.getCollectionElementType()));
+      case MAP:
+        return TypeDescriptors.maps(
+            javaTypeForFieldType(fieldType.getMapKeyType()),
+            javaTypeForFieldType(fieldType.getMapValueType()));
+      case ROW:
+        return TypeDescriptors.rows();
+      default:
+        return PRIMITIVE_MAPPING.get(fieldType.getTypeName());
+    }
+  }
+  /** Get a {@link FieldType} from a {@link TypeDescriptor}. */
+  public static FieldType fieldTypeForJavaType(TypeDescriptor typeDescriptor) {
+    if (typeDescriptor.isArray()
+        || typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
+      return getArrayFieldType(typeDescriptor);
+    } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Map.class))) {
+      return getMapFieldType(typeDescriptor);
+    } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Row.class))) {
+      throw new IllegalArgumentException(
+          "Cannot automatically determine a field type from a Row class"
+              + " as we cannot determine the schema. You should set a field type explicitly.");
+    } else {
+      TypeName typeName = PRIMITIVE_MAPPING.inverse().get(typeDescriptor);
+      if (typeName == null) {
+        throw new RuntimeException("Couldn't find field type for " + typeDescriptor);
+      }
+      FieldType fieldType = FieldType.of(typeName);
+      return fieldType;
+    }
+  }
+
+  private static FieldType getArrayFieldType(TypeDescriptor typeDescriptor) {
+    if (typeDescriptor.isArray()) {
+      if (typeDescriptor.getComponentType().getType().equals(byte.class)) {
+        return FieldType.BYTES;
+      } else {
+        return FieldType.array(fieldTypeForJavaType(typeDescriptor.getComponentType()));
+      }
+    }
+    if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
+      TypeDescriptor<Collection<?>> collection = typeDescriptor.getSupertype(Collection.class);
+      if (collection.getType() instanceof ParameterizedType) {
+        ParameterizedType ptype = (ParameterizedType) collection.getType();
+        java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
+        checkArgument(params.length == 1);
+        return FieldType.array(fieldTypeForJavaType(TypeDescriptor.of(params[0])));
+      }
+    }
+    throw new RuntimeException("Coupld not determine array parameter type for field.");
+  }
+
+  private static FieldType getMapFieldType(TypeDescriptor typeDescriptor) {
+    TypeDescriptor<Collection<?>> map = typeDescriptor.getSupertype(Map.class);
+    if (map.getType() instanceof ParameterizedType) {
+      ParameterizedType ptype = (ParameterizedType) map.getType();
+      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
+      return FieldType.map(
+          fieldTypeForJavaType(TypeDescriptor.of(params[0])),
+          fieldTypeForJavaType(TypeDescriptor.of(params[1])));
+    }
+    throw new RuntimeException("Cound not determine array parameter type for field.");
+  }
+}
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/values/TypeDescriptors.java
b/sdks/java/core/src/main/java/org/apache/beam/sdk/values/TypeDescriptors.java
index 46375c7eec8..d125356356a 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/values/TypeDescriptors.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/values/TypeDescriptors.java
@@ -22,6 +22,7 @@
 import java.math.BigDecimal;
 import java.math.BigInteger;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import org.apache.beam.sdk.transforms.Contextful;
 import org.apache.beam.sdk.transforms.SerializableFunction;
@@ -257,6 +258,15 @@
     return typeDescriptor;
   }
 
+  /** The {@link TypeDescriptor} for {@link Map}. */
+  public static <K, V> TypeDescriptor<Map<K, V>> maps(
+      TypeDescriptor<K> keyType, TypeDescriptor<V> valueType) {
+    TypeDescriptor<Map<K, V>> typeDescriptor =
+        new TypeDescriptor<Map<K, V>>() {}.where(new TypeParameter<K>()
{}, keyType)
+            .where(new TypeParameter<V>() {}, valueType);
+    return typeDescriptor;
+  }
+
   /**
    * The {@link TypeDescriptor} for {@link List}. This is the equivalent of:
    *
diff --git a/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldTypeDescriptorsTest.java
b/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldTypeDescriptorsTest.java
new file mode 100644
index 00000000000..16625ed8d28
--- /dev/null
+++ b/sdks/java/core/src/test/java/org/apache/beam/sdk/schemas/FieldTypeDescriptorsTest.java
@@ -0,0 +1,160 @@
+/*
+ * 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.transforms;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import org.apache.beam.sdk.schemas.FieldTypeDescriptors;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.Schema.FieldType;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.apache.beam.sdk.values.TypeDescriptors;
+import org.joda.time.Instant;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+/** test for {@link FieldTypeDescriptors}. */
+public class FieldTypeDescriptorsTest {
+  @Test
+  public void testPrimitiveTypeToJavaType() {
+    assertEquals(
+        TypeDescriptors.bytes(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.BYTE));
+    assertEquals(
+        TypeDescriptors.shorts(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.INT16));
+    assertEquals(
+        TypeDescriptors.integers(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.INT32));
+    assertEquals(
+        TypeDescriptors.longs(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.INT64));
+    assertEquals(
+        TypeDescriptors.bigdecimals(),
+        FieldTypeDescriptors.javaTypeForFieldType(FieldType.DECIMAL));
+    assertEquals(
+        TypeDescriptors.floats(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.FLOAT));
+    assertEquals(
+        TypeDescriptors.doubles(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.DOUBLE));
+    assertEquals(
+        TypeDescriptors.strings(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.STRING));
+    assertEquals(
+        TypeDescriptor.of(Instant.class),
+        FieldTypeDescriptors.javaTypeForFieldType(FieldType.DATETIME));
+    assertEquals(
+        TypeDescriptors.booleans(), FieldTypeDescriptors.javaTypeForFieldType(FieldType.BOOLEAN));
+    assertEquals(
+        TypeDescriptor.of(byte[].class),
+        FieldTypeDescriptors.javaTypeForFieldType(FieldType.BYTES));
+  }
+
+  @Test
+  public void testRowTypeToJavaType() {
+    assertEquals(
+        TypeDescriptors.lists(TypeDescriptors.rows()),
+        FieldTypeDescriptors.javaTypeForFieldType(
+            FieldType.array(FieldType.row(Schema.builder().build()))));
+  }
+
+  @Test
+  public void testArrayTypeToJavaType() {
+    assertEquals(
+        TypeDescriptors.lists(TypeDescriptors.longs()),
+        FieldTypeDescriptors.javaTypeForFieldType(FieldType.array(FieldType.INT64)));
+    assertEquals(
+        TypeDescriptors.lists(TypeDescriptors.lists(TypeDescriptors.longs())),
+        FieldTypeDescriptors.javaTypeForFieldType(
+            FieldType.array(FieldType.array(FieldType.INT64))));
+  }
+
+  @Test
+  public void testMapTypeToJavaType() {
+    assertEquals(
+        TypeDescriptors.maps(TypeDescriptors.strings(), TypeDescriptors.longs()),
+        FieldTypeDescriptors.javaTypeForFieldType(
+            FieldType.map(FieldType.STRING, FieldType.INT64)));
+    assertEquals(
+        TypeDescriptors.maps(
+            TypeDescriptors.strings(), TypeDescriptors.lists(TypeDescriptors.longs())),
+        FieldTypeDescriptors.javaTypeForFieldType(
+            FieldType.map(FieldType.STRING, FieldType.array(FieldType.INT64))));
+  }
+
+  @Test
+  public void testPrimitiveTypeToFieldType() {
+    assertEquals(
+        FieldType.BYTE, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.bytes()));
+    assertEquals(
+        FieldType.INT16, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.shorts()));
+    assertEquals(
+        FieldType.INT32, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.integers()));
+    assertEquals(
+        FieldType.INT64, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.longs()));
+    assertEquals(
+        FieldType.DECIMAL,
+        FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.bigdecimals()));
+    assertEquals(
+        FieldType.FLOAT, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.floats()));
+    assertEquals(
+        FieldType.DOUBLE, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.doubles()));
+    assertEquals(
+        FieldType.STRING, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.strings()));
+    assertEquals(
+        FieldType.DATETIME,
+        FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptor.of(Instant.class)));
+    assertEquals(
+        FieldType.BOOLEAN, FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.booleans()));
+    assertEquals(
+        FieldType.BYTES,
+        FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptor.of(byte[].class)));
+  }
+
+  @Rule public transient ExpectedException thrown = ExpectedException.none();
+
+  @Test
+  public void testRowTypeToFieldType() {
+    thrown.expect(IllegalArgumentException.class);
+    FieldTypeDescriptors.fieldTypeForJavaType(TypeDescriptors.rows());
+  }
+
+  @Test
+  public void testArrayTypeToFieldType() {
+    assertEquals(
+        FieldType.array(FieldType.STRING),
+        FieldTypeDescriptors.fieldTypeForJavaType(
+            TypeDescriptors.lists(TypeDescriptors.strings())));
+    assertEquals(
+        FieldType.array(FieldType.array(FieldType.STRING)),
+        FieldTypeDescriptors.fieldTypeForJavaType(
+            TypeDescriptors.lists(TypeDescriptors.lists(TypeDescriptors.strings()))));
+    assertEquals(
+        FieldType.array(FieldType.STRING),
+        FieldTypeDescriptors.fieldTypeForJavaType(
+            TypeDescriptor.of(new ArrayList<String>() {}.getClass())));
+  }
+
+  @Test
+  public void testMapTypeToFieldType() {
+    assertEquals(
+        FieldType.map(FieldType.STRING, FieldType.INT64),
+        FieldTypeDescriptors.fieldTypeForJavaType(
+            TypeDescriptors.maps(TypeDescriptors.strings(), TypeDescriptors.longs())));
+    assertEquals(
+        FieldType.map(FieldType.STRING, FieldType.array(FieldType.INT64)),
+        FieldTypeDescriptors.fieldTypeForJavaType(
+            TypeDescriptors.maps(
+                TypeDescriptors.strings(), TypeDescriptors.lists(TypeDescriptors.longs()))));
+  }
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


Issue Time Tracking
-------------------

    Worklog Id:     (was: 144520)
    Time Spent: 9h 10m  (was: 9h)

> Create a library of useful transforms that use schemas
> ------------------------------------------------------
>
>                 Key: BEAM-4461
>                 URL: https://issues.apache.org/jira/browse/BEAM-4461
>             Project: Beam
>          Issue Type: Sub-task
>          Components: sdk-java-core
>            Reporter: Reuven Lax
>            Assignee: Reuven Lax
>            Priority: Major
>          Time Spent: 9h 10m
>  Remaining Estimate: 0h
>
> e.g. JoinBy(fields). Project, Filter, etc.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Mime
View raw message