|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.connectors.hive.util; |
| 20 | + |
| 21 | +import org.apache.flink.core.fs.Path; |
| 22 | +import org.apache.flink.table.plan.stats.TableStats; |
| 23 | +import org.apache.flink.table.types.AtomicDataType; |
| 24 | +import org.apache.flink.table.types.DataType; |
| 25 | +import org.apache.flink.table.types.logical.RowType; |
| 26 | +import org.apache.flink.table.types.logical.VarCharType; |
| 27 | +import org.apache.flink.testutils.junit.utils.TempDirUtils; |
| 28 | + |
| 29 | +import org.apache.hadoop.conf.Configuration; |
| 30 | +import org.junit.jupiter.api.BeforeEach; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import org.junit.jupiter.api.io.TempDir; |
| 33 | + |
| 34 | +import java.io.File; |
| 35 | +import java.io.IOException; |
| 36 | +import java.net.URI; |
| 37 | +import java.nio.file.Files; |
| 38 | +import java.util.ArrayList; |
| 39 | +import java.util.List; |
| 40 | + |
| 41 | +import static java.nio.file.StandardOpenOption.APPEND; |
| 42 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 43 | + |
| 44 | +/** Tests for {@link TextFormatStatisticsReportUtil}. */ |
| 45 | +class TextFormatStatisticsReportUtilTest { |
| 46 | + private Configuration hadoopConfig; |
| 47 | + private DataType producedDataType; |
| 48 | + |
| 49 | + @TempDir private java.nio.file.Path temporaryFolder; |
| 50 | + |
| 51 | + @BeforeEach |
| 52 | + void setUp() { |
| 53 | + hadoopConfig = new Configuration(); |
| 54 | + // Create a sample producedDataType with a RowType |
| 55 | + List<RowType.RowField> fields = new ArrayList<>(); |
| 56 | + fields.add(new RowType.RowField("field1", new VarCharType())); |
| 57 | + fields.add(new RowType.RowField("field2", new VarCharType())); |
| 58 | + fields.add(new RowType.RowField("field3", new VarCharType())); |
| 59 | + producedDataType = new AtomicDataType(new RowType(fields)); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void testEstimateTableStatisticsCase1() throws IOException { |
| 64 | + // Create sample files for testing |
| 65 | + File tempFile = TempDirUtils.newFile(temporaryFolder, "flink_test_file.txt"); |
| 66 | + |
| 67 | + List<Path> files = new ArrayList<>(); |
| 68 | + files.add(new Path(tempFile.toURI())); |
| 69 | + |
| 70 | + String sampleString = "sample data"; |
| 71 | + Files.write(tempFile.toPath(), sampleString.getBytes()); |
| 72 | + TableStats stats = |
| 73 | + TextFormatStatisticsReportUtil.estimateTableStatistics( |
| 74 | + files, producedDataType, hadoopConfig); |
| 75 | + assertEquals(0, stats.getRowCount()); |
| 76 | + for (int i = 0; i < 10; ++i) { |
| 77 | + Files.write(tempFile.toPath(), sampleString.getBytes(), APPEND); |
| 78 | + } |
| 79 | + stats = |
| 80 | + TextFormatStatisticsReportUtil.estimateTableStatistics( |
| 81 | + files, producedDataType, hadoopConfig); |
| 82 | + assertEquals(3, stats.getRowCount()); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + void testEstimateFailedToUnknown() { |
| 87 | + List<Path> files = new ArrayList<>(); |
| 88 | + files.add(new Path(URI.create("file:///non_existent_file.txt"))); |
| 89 | + // Estimate table statistics |
| 90 | + TableStats stats = |
| 91 | + TextFormatStatisticsReportUtil.estimateTableStatistics( |
| 92 | + files, producedDataType, hadoopConfig); |
| 93 | + // Verify that it returns TableStats.UNKNOWN on failure |
| 94 | + assertEquals(TableStats.UNKNOWN, stats); |
| 95 | + } |
| 96 | +} |
0 commit comments