You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2021/01/07 03:48:25 UTC

[GitHub] [flink] maosuhan commented on a change in pull request #14376: [FLINK-18202][PB format] New Format of protobuf

maosuhan commented on a change in pull request #14376:
URL: https://github.com/apache/flink/pull/14376#discussion_r553093799



##########
File path: flink-formats/flink-protobuf/src/main/java/org/apache/flink/formats/protobuf/PbSchemaValidator.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.flink.formats.protobuf;
+
+import org.apache.flink.table.api.ValidationException;
+import org.apache.flink.table.types.logical.ArrayType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.LogicalTypeRoot;
+import org.apache.flink.table.types.logical.MapType;
+import org.apache.flink.table.types.logical.RowType;
+
+import com.google.protobuf.Descriptors;
+import com.google.protobuf.Descriptors.FieldDescriptor;
+import com.google.protobuf.Descriptors.FieldDescriptor.JavaType;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class PbSchemaValidator {
+	private Descriptors.Descriptor descriptor;
+	private RowType rowType;
+	private Map<JavaType, List<LogicalTypeRoot>> typeMatchMap = new HashMap();
+
+	public PbSchemaValidator(Descriptors.Descriptor descriptor, RowType rowType) {
+		this.descriptor = descriptor;
+		this.rowType = rowType;
+		typeMatchMap.put(JavaType.BOOLEAN, Collections.singletonList(LogicalTypeRoot.BOOLEAN));
+		typeMatchMap.put(
+			JavaType.BYTE_STRING,
+			Arrays.asList(LogicalTypeRoot.BINARY, LogicalTypeRoot.VARBINARY));
+		typeMatchMap.put(JavaType.DOUBLE, Collections.singletonList(LogicalTypeRoot.DOUBLE));
+		typeMatchMap.put(JavaType.FLOAT, Collections.singletonList(LogicalTypeRoot.FLOAT));
+		typeMatchMap.put(
+			JavaType.ENUM,
+			Arrays.asList(LogicalTypeRoot.VARCHAR, LogicalTypeRoot.CHAR));
+		typeMatchMap.put(
+			JavaType.STRING,
+			Arrays.asList(LogicalTypeRoot.VARCHAR, LogicalTypeRoot.CHAR));
+		typeMatchMap.put(JavaType.INT, Collections.singletonList(LogicalTypeRoot.INTEGER));
+		typeMatchMap.put(JavaType.LONG, Collections.singletonList(LogicalTypeRoot.BIGINT));
+	}
+
+	public Descriptors.Descriptor getDescriptor() {
+		return descriptor;
+	}
+
+	public void setDescriptor(Descriptors.Descriptor descriptor) {
+		this.descriptor = descriptor;
+	}
+
+	public RowType getRowType() {
+		return rowType;
+	}
+
+	public void setRowType(RowType rowType) {
+		this.rowType = rowType;
+	}
+
+	public void validate() {
+		validateTypeMatch(descriptor, rowType);
+		if (!descriptor.getFile().getOptions().getJavaPackage()

Review comment:
       This is a good question. If we omit java_package or set a different value with package, it is a little complicated to find out the real java type of nested message.
   For example,
        ```
        package foo.bar
   
        option java_package = "com.example.protos"
   
        message A{
   
             message B{}
   
        }
       ```
   When we get Descriptor of B, we cannot get the real java class name directly by using com.google.protobuf.Descriptors.Descriptor.getName(), the name is comming from package instead of java_package.
   So I just make it simple by forcibly set package the same with java_package.
   
   I tried to make it flexible to set different values, but it made the code a little complicated and not very compatible.
   
   Do you have any good ideas?
   
   




----------------------------------------------------------------
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