twalthr commented on a change in pull request #9884: [FLINK-14266][table] 
Introduce RowCsvInputFormat to new CSV module
URL: https://github.com/apache/flink/pull/9884#discussion_r336064018
 
 

 ##########
 File path: 
flink-formats/flink-csv/src/test/java/org/apache/flink/formats/csv/RowCsvInputFormatTest.java
 ##########
 @@ -0,0 +1,771 @@
+/*
+ * 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.csv;
+
+import org.apache.flink.api.common.typeinfo.BasicTypeInfo;
+import org.apache.flink.api.common.typeinfo.SqlTimeTypeInfo;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.core.fs.FileInputSplit;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.types.Row;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.sql.Date;
+import java.sql.Time;
+import java.sql.Timestamp;
+
+import static junit.framework.TestCase.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+/**
+ * Tests for {@link RowCsvInputFormat}.
+ */
+public class RowCsvInputFormatTest {
+
+       static final Path PATH = new Path("an/ignored/file/");
+
+       // static variables for testing the removal of \r\n to \n
+       private static final String FIRST_PART = "That is the first part";
+       private static final String SECOND_PART = "That is the second part";
+
+       @Test
+       public void ignoreInvalidLines() throws Exception {
+               String fileContent =
+                       "#description of the data\n" +
+                               "header1|header2|header3|\n" +
+                               "this is|1|2.0|\n" +
+                               "//a comment\n" +
+                               "a test|3|4.0|\n" +
+                               "#next|5|6.0|\n";
+
+               FileInputSplit split = createTempFile(fileContent);
+
+               TypeInformation[] fieldTypes = new 
TypeInformation[]{BasicTypeInfo.STRING_TYPE_INFO, BasicTypeInfo.INT_TYPE_INFO, 
BasicTypeInfo.DOUBLE_TYPE_INFO};
+               RowCsvInputFormat.Builder builder = 
RowCsvInputFormat.builder(new RowTypeInfo(fieldTypes), PATH)
+                               .setFieldDelimiter('|')
+                               .setIgnoreParseErrors(false);
+
+               RowCsvInputFormat format = builder.build();
+               Configuration parameters = new Configuration();
+               format.configure(parameters);
+               format.open(split);
+
+               Row result = new Row(3);
+               try {
+                       result = format.nextRecord(result);
+                       fail("RuntimeException was not thrown! (Row length 
mismatch. 3 fields expected but was 1)");
+               } catch (IOException ignored) {
+               } // => ok
+
+               try {
+                       result = format.nextRecord(result);
+                       fail("NumberFormatException was not thrown! (For input 
string: \"header2\")");
+               } catch (IOException ignored) {
+               } // => ok
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("this is", result.getField(0));
+               assertEquals(1, result.getField(1));
+               assertEquals(2.0, result.getField(2));
+
+               try {
+                       result = format.nextRecord(result);
+                       fail("RuntimeException was not thrown! (Row length 
mismatch. 3 fields expected but was 1)");
+               } catch (IOException ignored) {
+               } // => ok
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("a test", result.getField(0));
+               assertEquals(3, result.getField(1));
+               assertEquals(4.0, result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("#next", result.getField(0));
+               assertEquals(5, result.getField(1));
+               assertEquals(6.0, result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNull(result);
+
+               // re-open with lenient = true
+               builder.setIgnoreParseErrors(true);
+               format = builder.build();
+               format.configure(parameters);
+               format.open(split);
+
+               result = new Row(3);
+
+               // NOTE: old csv will skip this too short row.
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("#description of the data", result.getField(0));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("header1", result.getField(0));
+               assertNull(result.getField(1));
+               assertNull(result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("this is", result.getField(0));
+               assertEquals(1, result.getField(1));
+               assertEquals(2.0, result.getField(2));
+
+               // NOTE: old csv will skip this too short row.
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("//a comment", result.getField(0));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("a test", result.getField(0));
+               assertEquals(3, result.getField(1));
+               assertEquals(4.0, result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("#next", result.getField(0));
+               assertEquals(5, result.getField(1));
+               assertEquals(6.0, result.getField(2));
+               result = format.nextRecord(result);
+               assertNull(result);
+       }
+
+       @Test
+       public void ignorePrefixComments() throws Exception {
+               String fileContent =
+                       "#description of the data\n" +
+                               "#successive commented line\n" +
+                               "this is|1|2.0|\n" +
+                               "a test|3|4.0|\n" +
+                               "#next|5|6.0|\n";
+
+               FileInputSplit split = createTempFile(fileContent);
+
+               TypeInformation[] fieldTypes = new TypeInformation[]{
+                               BasicTypeInfo.STRING_TYPE_INFO,
+                               BasicTypeInfo.INT_TYPE_INFO,
+                               BasicTypeInfo.DOUBLE_TYPE_INFO};
+
+               RowCsvInputFormat.Builder builder = 
RowCsvInputFormat.builder(new RowTypeInfo(fieldTypes), PATH)
+                               .setFieldDelimiter('|')
+                               .setAllowComments(true);
+
+               RowCsvInputFormat format = builder.build();
+               format.configure(new Configuration());
+               format.open(split);
+
+               Row result = new Row(3);
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("this is", result.getField(0));
+               assertEquals(1, result.getField(1));
+               assertEquals(2.0, result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("a test", result.getField(0));
+               assertEquals(3, result.getField(1));
+               assertEquals(4.0, result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNull(result);
+       }
+
+       // NOTE: new csv not support configure comments char.
+
+       @Test
+       public void readStringFields() throws Exception {
+               String fileContent = "abc|def|ghijk\nabc||hhg\n|||\n||";
+
+               FileInputSplit split = createTempFile(fileContent);
+
+               TypeInformation[] fieldTypes = new TypeInformation[]{
+                               BasicTypeInfo.STRING_TYPE_INFO,
+                               BasicTypeInfo.STRING_TYPE_INFO,
+                               BasicTypeInfo.STRING_TYPE_INFO};
+
+               RowCsvInputFormat.Builder builder = 
RowCsvInputFormat.builder(new RowTypeInfo(fieldTypes), PATH)
+                               .setFieldDelimiter('|');
+
+               RowCsvInputFormat format = builder.build();
+               format.configure(new Configuration());
+               format.open(split);
+
+               Row result = new Row(3);
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("abc", result.getField(0));
+               assertEquals("def", result.getField(1));
+               assertEquals("ghijk", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("abc", result.getField(0));
+               assertEquals("", result.getField(1));
+               assertEquals("hhg", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("", result.getField(0));
+               assertEquals("", result.getField(1));
+               assertEquals("", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("", result.getField(0));
+               assertEquals("", result.getField(1));
+               assertEquals("", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNull(result);
+               assertTrue(format.reachedEnd());
+       }
+
+       @Test
+       public void readMixedQuotedStringFields() throws Exception {
+               String fileContent = "@a|b|c@|def|@ghijk@\nabc||@|hhg@\n|||\n";
+
+               FileInputSplit split = createTempFile(fileContent);
+
+               TypeInformation[] fieldTypes = new TypeInformation[]{
+                               BasicTypeInfo.STRING_TYPE_INFO,
+                               BasicTypeInfo.STRING_TYPE_INFO,
+                               BasicTypeInfo.STRING_TYPE_INFO};
+
+               RowCsvInputFormat.Builder builder = 
RowCsvInputFormat.builder(new RowTypeInfo(fieldTypes), PATH)
+                               .setFieldDelimiter('|')
+                               .setQuoteCharacter('@');
+
+               RowCsvInputFormat format = builder.build();
+               format.configure(new Configuration());
+               format.open(split);
+
+               Row result = new Row(3);
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("a|b|c", result.getField(0));
+               assertEquals("def", result.getField(1));
+               assertEquals("ghijk", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("abc", result.getField(0));
+               assertEquals("", result.getField(1));
+               assertEquals("|hhg", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNotNull(result);
+               assertEquals("", result.getField(0));
+               assertEquals("", result.getField(1));
+               assertEquals("", result.getField(2));
+
+               result = format.nextRecord(result);
+               assertNull(result);
+               assertTrue(format.reachedEnd());
+       }
+
+       // NOTE: new csv not support configure multi chars field delimiter.
 
 Review comment:
   all limitations mentioned here should also be mentioned in the input format 
class as well as in the descriptor

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


With regards,
Apache Git Services

Reply via email to