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

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

                Author: ASF GitHub Bot
            Created on: 02/Jun/19 10:33
            Start Date: 02/Jun/19 10:33
    Worklog Time Spent: 10m 
      Work Description: reuvenlax commented on pull request #8690: [BEAM-7274] 
Implement the Protobuf schema provider
URL: https://github.com/apache/beam/pull/8690#discussion_r287846932
 
 

 ##########
 File path: 
sdks/java/extensions/protobuf/src/test/java/org/apache/beam/sdk/extensions/protobuf/ProtoSchemaTest.java
 ##########
 @@ -0,0 +1,506 @@
+/*
+ * 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.extensions.protobuf;
+
+import static org.junit.Assert.assertEquals;
+
+import com.google.protobuf.Descriptors;
+import com.google.protobuf.DynamicMessage;
+import com.google.protobuf.Message;
+import java.util.Objects;
+import org.apache.beam.sdk.coders.RowCoder;
+import org.apache.beam.sdk.schemas.Schema;
+import org.apache.beam.sdk.schemas.SchemaCoder;
+import org.apache.beam.sdk.transforms.SerializableFunction;
+import org.apache.beam.sdk.util.CoderUtils;
+import org.apache.beam.sdk.util.SerializableUtils;
+import org.apache.beam.sdk.values.Row;
+import org.apache.beam.sdk.values.TypeDescriptor;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+/** Collection of standard tests for Protobuf Schema support. */
+@RunWith(JUnit4.class)
+public class ProtoSchemaTest {
+
+  private static final Schema PRIMITIVE_SCHEMA =
+      Schema.builder()
+          .addDoubleField("primitive_double")
+          .addFloatField("primitive_float")
+          .addInt32Field("primitive_int32")
+          .addInt64Field("primitive_int64")
+          .addInt32Field("primitive_uint32")
+          .addInt64Field("primitive_uint64")
+          .addInt32Field("primitive_sint32")
+          .addInt64Field("primitive_sint64")
+          .addInt32Field("primitive_fixed32")
+          .addInt64Field("primitive_fixed64")
+          .addInt32Field("primitive_sfixed32")
+          .addInt64Field("primitive_sfixed64")
+          .addBooleanField("primitive_bool")
+          .addStringField("primitive_string")
+          .addByteArrayField("primitive_bytes")
+          .build();
+  static final Row PRIMITIVE_DEFAULT_ROW =
+      Row.withSchema(PRIMITIVE_SCHEMA)
+          .addValue((double) 0)
+          .addValue((float) 0)
+          .addValue(0)
+          .addValue(0L)
+          .addValue(0)
+          .addValue(0L)
+          .addValue(0)
+          .addValue(0L)
+          .addValue(0)
+          .addValue(0L)
+          .addValue(0)
+          .addValue(0L)
+          .addValue(Boolean.FALSE)
+          .addValue("")
+          .addValue(new byte[] {})
+          .build();
+  static final Schema MESSAGE_SCHEMA =
+      Schema.builder()
+          .addField("message", 
Schema.FieldType.row(PRIMITIVE_SCHEMA).withNullable(true))
+          .addField(
+              "repeated_message",
+              Schema.FieldType.array(
+                      // TODO: are the nullable's correct
+                      
Schema.FieldType.row(PRIMITIVE_SCHEMA).withNullable(true))
+                  .withNullable(true))
+          .build();
+  private static final Row MESSAGE_DEFAULT_ROW =
+      Row.withSchema(MESSAGE_SCHEMA).addValue(null).addValue(null).build();
+  private static final Schema REPEAT_PRIMITIVE_SCHEMA =
+      Schema.builder()
+          .addField(
+              "repeated_double", 
Schema.FieldType.array(Schema.FieldType.DOUBLE).withNullable(true))
+          .addField(
+              "repeated_float", 
Schema.FieldType.array(Schema.FieldType.FLOAT).withNullable(true))
+          .addField(
+              "repeated_int32", 
Schema.FieldType.array(Schema.FieldType.INT32).withNullable(true))
+          .addField(
+              "repeated_int64", 
Schema.FieldType.array(Schema.FieldType.INT64).withNullable(true))
+          .addField(
+              "repeated_uint32", 
Schema.FieldType.array(Schema.FieldType.INT32).withNullable(true))
+          .addField(
+              "repeated_uint64", 
Schema.FieldType.array(Schema.FieldType.INT64).withNullable(true))
+          .addField(
+              "repeated_sint32", 
Schema.FieldType.array(Schema.FieldType.INT32).withNullable(true))
+          .addField(
+              "repeated_sint64", 
Schema.FieldType.array(Schema.FieldType.INT64).withNullable(true))
+          .addField(
+              "repeated_fixed32", 
Schema.FieldType.array(Schema.FieldType.INT32).withNullable(true))
+          .addField(
+              "repeated_fixed64", 
Schema.FieldType.array(Schema.FieldType.INT64).withNullable(true))
+          .addField(
+              "repeated_sfixed32",
+              
Schema.FieldType.array(Schema.FieldType.INT32).withNullable(true))
+          .addField(
+              "repeated_sfixed64",
+              
Schema.FieldType.array(Schema.FieldType.INT64).withNullable(true))
+          .addField(
+              "repeated_bool", 
Schema.FieldType.array(Schema.FieldType.BOOLEAN).withNullable(true))
+          .addField(
+              "repeated_string", 
Schema.FieldType.array(Schema.FieldType.STRING).withNullable(true))
+          .addField(
+              "repeated_bytes", 
Schema.FieldType.array(Schema.FieldType.BYTES).withNullable(true))
+          .build();
+  static final Row REPEAT_PRIMITIVE_DEFAULT_ROW =
+      Row.withSchema(REPEAT_PRIMITIVE_SCHEMA)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .build();
+  private static final Schema COMPLEX_SCHEMA =
+      Schema.builder()
+          .addField("special_enum", Schema.FieldType.STRING)
+          .addField(
+              "repeated_enum", 
Schema.FieldType.array(Schema.FieldType.STRING).withNullable(true))
+          .addField("oneof_int32", Schema.FieldType.INT32.withNullable(true))
+          .addField("oneof_bool", Schema.FieldType.BOOLEAN.withNullable(true))
+          .addField("oneof_string", Schema.FieldType.STRING.withNullable(true))
+          .addField("oneof_primitive", 
Schema.FieldType.row(PRIMITIVE_SCHEMA).withNullable(true))
+          .addField(
+              "x",
+              Schema.FieldType.map(Schema.FieldType.STRING, 
Schema.FieldType.INT32)
+                  .withNullable(true))
+          .addField(
+              "y",
+              Schema.FieldType.map(Schema.FieldType.STRING, 
Schema.FieldType.STRING)
+                  .withNullable(true))
+          .addField(
+              "z",
+              // TODO: null in map, does it make sense.
+              Schema.FieldType.map(
+                      Schema.FieldType.STRING,
+                      
Schema.FieldType.row(PRIMITIVE_SCHEMA).withNullable(true))
+                  .withNullable(true))
+          .addField("oneof_int64", Schema.FieldType.INT64.withNullable(true))
+          .addField("oneof_double", Schema.FieldType.DOUBLE.withNullable(true))
+          .build();
+  static final Row COMPLEX_DEFAULT_ROW =
+      Row.withSchema(COMPLEX_SCHEMA)
+          .addValue("UNKNOWN")
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .build();
+  private static final Schema WKT_MESSAGE_SCHEMA =
+      Schema.builder()
+          .addField("nullable_double", 
Schema.FieldType.DOUBLE.withNullable(true))
+          .addField("nullable_float", 
Schema.FieldType.FLOAT.withNullable(true))
+          .addField("nullable_int32", 
Schema.FieldType.INT32.withNullable(true))
+          .addField("nullable_int64", 
Schema.FieldType.INT64.withNullable(true))
+          .addField("nullable_uint32", 
Schema.FieldType.INT32.withNullable(true))
+          .addField("nullable_uint64", 
Schema.FieldType.INT64.withNullable(true))
+          // xxx
+          .addField("nullable_bool", 
Schema.FieldType.BOOLEAN.withNullable(true))
+          .addField("nullable_string", 
Schema.FieldType.STRING.withNullable(true))
+          .addField("nullable_bytes", 
Schema.FieldType.BYTES.withNullable(true))
+          //
+          .addField("wkt_timestamp", 
Schema.FieldType.DATETIME.withNullable(true))
+          .build();
+  static final Row WKT_MESSAGE_DEFAULT_ROW =
+      Row.withSchema(WKT_MESSAGE_SCHEMA)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .addValue(null)
+          .build();
+
+  @Test
+  public void testPrimitiveSchema() {
+    Schema schema =
+        new ProtoSchemaProvider()
+            
.schemaFor(TypeDescriptor.of(Proto3SchemaMessages.Primitive.class));
+    assertEquals(PRIMITIVE_SCHEMA, schema);
+  }
+
+  @Test
+  public void testPrimitiveDefaultRow() {
+    SerializableFunction<Proto3SchemaMessages.Primitive, Row> toRowFunction =
+        new ProtoSchemaProvider()
+            
.toRowFunction(TypeDescriptor.of(Proto3SchemaMessages.Primitive.class));
+    Row row = 
toRowFunction.apply(Proto3SchemaMessages.Primitive.newBuilder().build());
+    assertEquals(PRIMITIVE_DEFAULT_ROW, row);
+  }
+
+  @Test
+  public void testMessageSchema() {
+    Schema schema =
+        new 
ProtoSchemaProvider().schemaFor(TypeDescriptor.of(Proto3SchemaMessages.Message.class));
+    assertEquals(MESSAGE_SCHEMA, schema);
+  }
+
+  @Test
+  public void testMessageDefaultRow() {
+    SerializableFunction<Proto3SchemaMessages.Message, Row> toRowFunction =
+        new ProtoSchemaProvider()
+            
.toRowFunction(TypeDescriptor.of(Proto3SchemaMessages.Message.class));
+    Row row = 
toRowFunction.apply(Proto3SchemaMessages.Message.newBuilder().build());
+    assertEquals(MESSAGE_DEFAULT_ROW, row);
+  }
+
+  @Test
+  public void testRepeatPrimitiveSchema() {
+    Schema schema =
+        new ProtoSchemaProvider()
+            
.schemaFor(TypeDescriptor.of(Proto3SchemaMessages.RepeatPrimitive.class));
+    assertEquals(REPEAT_PRIMITIVE_SCHEMA, schema);
+  }
+
+  @Test
+  public void testRepeatPrimitiveDefaultRow() {
+    SerializableFunction<Proto3SchemaMessages.RepeatPrimitive, Row> 
toRowFunction =
+        new ProtoSchemaProvider()
+            
.toRowFunction(TypeDescriptor.of(Proto3SchemaMessages.RepeatPrimitive.class));
+    Row row = 
toRowFunction.apply(Proto3SchemaMessages.RepeatPrimitive.newBuilder().build());
+    assertEquals(REPEAT_PRIMITIVE_DEFAULT_ROW, row);
+  }
+
+  @Test
+  public void testComplexSchema() {
+    Schema schema =
+        new 
ProtoSchemaProvider().schemaFor(TypeDescriptor.of(Proto3SchemaMessages.Complex.class));
+    assertEquals(COMPLEX_SCHEMA, schema);
+  }
+
+  @Test
+  public void testComplexDefaultRow() {
+    SerializableFunction<Proto3SchemaMessages.Complex, Row> toRowFunction =
+        new ProtoSchemaProvider()
+            
.toRowFunction(TypeDescriptor.of(Proto3SchemaMessages.Complex.class));
+    Row row = 
toRowFunction.apply(Proto3SchemaMessages.Complex.newBuilder().build());
+    assertEquals(COMPLEX_DEFAULT_ROW, row);
+  }
+
+  @Test
+  public void testWktMessageSchema() {
+    Schema schema =
+        new ProtoSchemaProvider()
+            
.schemaFor(TypeDescriptor.of(Proto3SchemaMessages.WktMessage.class));
+    assertEquals(WKT_MESSAGE_SCHEMA, schema);
+  }
+
+  @Test
+  public void testWktMessageDefaultRow() {
+    SerializableFunction<Proto3SchemaMessages.WktMessage, Row> toRowFunction =
+        new ProtoSchemaProvider()
+            
.toRowFunction(TypeDescriptor.of(Proto3SchemaMessages.WktMessage.class));
+    Row row = 
toRowFunction.apply(Proto3SchemaMessages.WktMessage.newBuilder().build());
+    assertEquals(WKT_MESSAGE_DEFAULT_ROW, row);
+  }
+
+  @Test
+  public void testCoder() throws Exception {
+    ProtoSchemaProvider provider = new ProtoSchemaProvider();
+    TypeDescriptor typeDescriptor = 
TypeDescriptor.of(Proto3SchemaMessages.Complex.class);
+    SchemaCoder<Message> schemaCoder =
+        SchemaCoder.of(
+            provider.schemaFor(typeDescriptor),
+            provider.toRowFunction(typeDescriptor),
+            provider.fromRowFunction(typeDescriptor));
+
+    provider.add("", Proto3SchemaMessages.Complex.getDescriptor());
+    RowCoder rowCoder = RowCoder.of(schemaCoder.getSchema());
+
+    byte[] schemaCoderBytes = 
SerializableUtils.serializeToByteArray(schemaCoder);
+    SchemaCoder<Message> schemaCoderCoded =
+        (SchemaCoder<Message>) 
SerializableUtils.deserializeFromByteArray(schemaCoderBytes, "");
+    byte[] rowCoderBytes = SerializableUtils.serializeToByteArray(rowCoder);
+    RowCoder rowCoderCoded =
+        (RowCoder) SerializableUtils.deserializeFromByteArray(rowCoderBytes, 
"");
 
 Review comment:
   What is the above testing?
 
----------------------------------------------------------------
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:
[email protected]


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

    Worklog Id:     (was: 252823)
    Time Spent: 1h 20m  (was: 1h 10m)

> Protobuf Beam Schema support
> ----------------------------
>
>                 Key: BEAM-7274
>                 URL: https://issues.apache.org/jira/browse/BEAM-7274
>             Project: Beam
>          Issue Type: Improvement
>          Components: sdk-java-core
>            Reporter: Alex Van Boxel
>            Assignee: Alex Van Boxel
>            Priority: Minor
>          Time Spent: 1h 20m
>  Remaining Estimate: 0h
>
> Add support for the new Beam Schema to the Protobuf extension.



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

Reply via email to