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 2022/06/27 12:31:14 UTC

[GitHub] [flink] godfreyhe commented on a diff in pull request #20007: [FLINK-27989][table-planner] Csv format supports reporting statistics

godfreyhe commented on code in PR #20007:
URL: https://github.com/apache/flink/pull/20007#discussion_r907331668


##########
flink-formats/flink-csv/src/test/java/org/apache/flink/formats/csv/CsvFormatStatisticsReportTest.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.configuration.Configuration;
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.table.plan.stats.TableStats;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/** Test for statistics functionality in {@link CsvFormatFactory}. */
+public class CsvFormatStatisticsReportTest {

Review Comment:
   please add some tests which input files have more than 100 lines



##########
flink-formats/flink-csv/src/main/java/org/apache/flink/formats/csv/CsvFileFormatFactory.java:
##########
@@ -136,6 +150,52 @@ public BulkFormat<RowData, FileSourceSplit> createRuntimeDecoder(
         public ChangelogMode getChangelogMode() {
             return ChangelogMode.insertOnly();
         }
+
+        @Override
+        public TableStats reportStatistics(List<Path> files, DataType producedDataType) {
+            final int totalSampleLineCnt = 100;

Review Comment:
   add some comment to explain why we use the magic number 100 here



##########
flink-formats/flink-csv/src/main/java/org/apache/flink/formats/csv/CsvFileFormatFactory.java:
##########
@@ -136,6 +150,52 @@ public BulkFormat<RowData, FileSourceSplit> createRuntimeDecoder(
         public ChangelogMode getChangelogMode() {
             return ChangelogMode.insertOnly();
         }
+
+        @Override
+        public TableStats reportStatistics(List<Path> files, DataType producedDataType) {
+            final int totalSampleLineCnt = 100;
+            try {
+                long totalFileSize = 0;
+                int sampledRowCnt = 0;
+                long sampledRowSize = 0;
+                for (Path file : files) {
+                    FileSystem fs = FileSystem.get(file.toUri());
+                    FileStatus status = fs.getFileStatus(file);
+                    totalFileSize += status.getLen();
+
+                    // sample the line size
+                    if (sampledRowCnt < totalSampleLineCnt) {
+                        try (InputStreamReader isr =
+                                        new InputStreamReader(
+                                                Files.newInputStream(
+                                                        new File(file.toUri()).toPath()));
+                                BufferedReader br = new BufferedReader(isr)) {
+                            String line;
+                            while (sampledRowCnt < totalSampleLineCnt
+                                    && (line = br.readLine()) != null) {
+                                sampledRowCnt += 1;
+                                sampledRowSize += line.getBytes(StandardCharsets.UTF_8).length;

Review Comment:
   please update the TestCsvInputFormat



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