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/10/21 08:19:47 UTC

[GitHub] [flink] twalthr commented on a change in pull request #17515: [FLINK-24575][table-runtime] Migrate TestRowDataCsvInputFormat to DeserializationSchemaFactory

twalthr commented on a change in pull request #17515:
URL: https://github.com/apache/flink/pull/17515#discussion_r733431078



##########
File path: flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/filesystem/TestCsvDeserializationSchema.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.table.filesystem;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.util.DataFormatConverters;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.parser.FieldParser;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * The {@link DeserializationSchema} that output {@link RowData}.
+ *
+ * <p>NOTE: This is meant only for testing purpose and doesn't provide a feature complete stable csv
+ * parser! If you need a feature complete CSV parser, check out the flink-csv package.
+ */
+public class TestCsvDeserializationSchema implements DeserializationSchema<RowData> {
+
+    private final DataFormatConverters.DataFormatConverter[] csvRowToRowDataConverters;
+    private final TypeInformation<?> typeInfo;
+    private final int fieldCount;
+    private final List<DataType> fieldTypes;
+
+    private transient FieldParser<?>[] fieldParsers;
+
+    public TestCsvDeserializationSchema(DataType schema) {
+        this.fieldTypes = DataType.getFieldDataTypes(schema);
+        this.fieldCount = fieldTypes.size();
+        this.typeInfo = TypeConversions.fromDataTypeToLegacyInfo(schema);
+
+        TypeInformation<?>[] fieldsTypeInfos =
+                TypeConversions.fromDataTypeToLegacyInfo(fieldTypes.toArray(new DataType[0]));

Review comment:
       instead of further using a deprecated method, can we simply add a map to this class that maps `LogicalTypeRoot` to `Class` for `FieldParser.getParserForType`?

##########
File path: flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/filesystem/TestCsvDeserializationSchema.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.table.filesystem;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.util.DataFormatConverters;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.parser.FieldParser;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * The {@link DeserializationSchema} that output {@link RowData}.
+ *
+ * <p>NOTE: This is meant only for testing purpose and doesn't provide a feature complete stable csv
+ * parser! If you need a feature complete CSV parser, check out the flink-csv package.
+ */
+public class TestCsvDeserializationSchema implements DeserializationSchema<RowData> {
+
+    private final DataFormatConverters.DataFormatConverter[] csvRowToRowDataConverters;
+    private final TypeInformation<?> typeInfo;
+    private final int fieldCount;
+    private final List<DataType> fieldTypes;
+
+    private transient FieldParser<?>[] fieldParsers;
+
+    public TestCsvDeserializationSchema(DataType schema) {
+        this.fieldTypes = DataType.getFieldDataTypes(schema);
+        this.fieldCount = fieldTypes.size();
+        this.typeInfo = TypeConversions.fromDataTypeToLegacyInfo(schema);
+
+        TypeInformation<?>[] fieldsTypeInfos =
+                TypeConversions.fromDataTypeToLegacyInfo(fieldTypes.toArray(new DataType[0]));
+        this.fieldParsers = new FieldParser<?>[fieldCount];
+        for (int i = 0; i < fieldsTypeInfos.length; i++) {
+            if (fieldsTypeInfos[i] != null) {
+                Class<? extends FieldParser<?>> parserType =
+                        FieldParser.getParserForType(fieldsTypeInfos[i].getTypeClass());
+                if (parserType == null) {
+                    throw new RuntimeException(
+                            "No parser available for type '"
+                                    + fieldsTypeInfos[i].getTypeClass().getName()
+                                    + "'.");
+                }
+
+                FieldParser<?> p = InstantiationUtil.instantiate(parserType, FieldParser.class);
+
+                this.fieldParsers[i] = p;
+            }
+        }
+
+        this.csvRowToRowDataConverters =
+                fieldTypes.stream()
+                        .map(DataFormatConverters::getConverterForDataType)

Review comment:
       is it possible to use `DataStructureConverter` instead? or would that break tests?

##########
File path: flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/filesystem/TestCsvDeserializationSchema.java
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.table.filesystem;
+
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.util.DataFormatConverters;
+import org.apache.flink.table.types.DataType;
+import org.apache.flink.table.types.utils.TypeConversions;
+import org.apache.flink.types.parser.FieldParser;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.IOException;
+import java.util.List;
+
+/**
+ * The {@link DeserializationSchema} that output {@link RowData}.
+ *
+ * <p>NOTE: This is meant only for testing purpose and doesn't provide a feature complete stable csv
+ * parser! If you need a feature complete CSV parser, check out the flink-csv package.
+ */
+public class TestCsvDeserializationSchema implements DeserializationSchema<RowData> {
+
+    private final DataFormatConverters.DataFormatConverter[] csvRowToRowDataConverters;
+    private final TypeInformation<?> typeInfo;
+    private final int fieldCount;
+    private final List<DataType> fieldTypes;
+
+    private transient FieldParser<?>[] fieldParsers;
+
+    public TestCsvDeserializationSchema(DataType schema) {
+        this.fieldTypes = DataType.getFieldDataTypes(schema);
+        this.fieldCount = fieldTypes.size();
+        this.typeInfo = TypeConversions.fromDataTypeToLegacyInfo(schema);
+
+        TypeInformation<?>[] fieldsTypeInfos =
+                TypeConversions.fromDataTypeToLegacyInfo(fieldTypes.toArray(new DataType[0]));
+        this.fieldParsers = new FieldParser<?>[fieldCount];
+        for (int i = 0; i < fieldsTypeInfos.length; i++) {
+            if (fieldsTypeInfos[i] != null) {
+                Class<? extends FieldParser<?>> parserType =
+                        FieldParser.getParserForType(fieldsTypeInfos[i].getTypeClass());
+                if (parserType == null) {
+                    throw new RuntimeException(
+                            "No parser available for type '"
+                                    + fieldsTypeInfos[i].getTypeClass().getName()
+                                    + "'.");
+                }
+
+                FieldParser<?> p = InstantiationUtil.instantiate(parserType, FieldParser.class);
+
+                this.fieldParsers[i] = p;
+            }
+        }
+
+        this.csvRowToRowDataConverters =
+                fieldTypes.stream()
+                        .map(DataFormatConverters::getConverterForDataType)
+                        .toArray(DataFormatConverters.DataFormatConverter[]::new);
+    }
+
+    @Override
+    public void open(InitializationContext context) throws Exception {
+        TypeInformation<?>[] fieldsTypeInfos =
+                TypeConversions.fromDataTypeToLegacyInfo(fieldTypes.toArray(new DataType[0]));
+        this.fieldParsers = new FieldParser<?>[fieldCount];
+        for (int i = 0; i < fieldsTypeInfos.length; i++) {
+            if (fieldsTypeInfos[i] != null) {
+                Class<? extends FieldParser<?>> parserType =

Review comment:
       avoid duplicate code




-- 
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: issues-unsubscribe@flink.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org