|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.tests.product.hive; |
| 15 | + |
| 16 | +import com.google.inject.Inject; |
| 17 | +import io.trino.tempto.ProductTest; |
| 18 | +import io.trino.tempto.assertions.QueryAssert; |
| 19 | +import io.trino.tempto.hadoop.hdfs.HdfsClient; |
| 20 | +import org.assertj.core.api.Assertions; |
| 21 | +import org.testng.annotations.Test; |
| 22 | + |
| 23 | +import java.io.ByteArrayInputStream; |
| 24 | +import java.io.ByteArrayOutputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.List; |
| 27 | + |
| 28 | +import static io.trino.tempto.assertions.QueryAssert.Row.row; |
| 29 | +import static io.trino.tempto.assertions.QueryAssert.assertThat; |
| 30 | +import static io.trino.testing.TestingNames.randomNameSuffix; |
| 31 | +import static io.trino.tests.product.hive.util.TableLocationUtils.getTablePath; |
| 32 | +import static io.trino.tests.product.utils.QueryExecutors.onHive; |
| 33 | +import static io.trino.tests.product.utils.QueryExecutors.onTrino; |
| 34 | + |
| 35 | +public class TestHiveHiddenFiles |
| 36 | + extends ProductTest |
| 37 | +{ |
| 38 | + @Inject |
| 39 | + private HdfsClient hdfsClient; |
| 40 | + |
| 41 | + @Test |
| 42 | + public void testSelectFromTableContainingHiddenFiles() |
| 43 | + throws Exception |
| 44 | + { |
| 45 | + String tableName = "test_table_hidden_files" + randomNameSuffix(); |
| 46 | + onTrino().executeQuery("CREATE TABLE " + tableName + " (col integer)"); |
| 47 | + |
| 48 | + onTrino().executeQuery("INSERT INTO " + tableName + " VALUES 1"); |
| 49 | + onTrino().executeQuery("INSERT INTO " + tableName + " VALUES 2"); |
| 50 | + |
| 51 | + List<QueryAssert.Row> tableRows = List.of(row(1), row(2)); |
| 52 | + assertThat(onTrino().executeQuery("SELECT * FROM " + tableName)).containsOnly(tableRows); |
| 53 | + assertThat(onHive().executeQuery("SELECT * FROM " + tableName)).containsOnly(tableRows); |
| 54 | + |
| 55 | + String tableLocation = getTablePath(tableName); |
| 56 | + // Rename the table files to Hive hidden tableFiles (prefixed by `.` or `_` characters) |
| 57 | + List<String> tableFiles = hdfsClient.listDirectory(tableLocation); |
| 58 | + Assertions.assertThat(tableFiles).hasSize(2); |
| 59 | + renameFile(tableLocation, tableFiles.get(0), '.' + tableFiles.get(0)); |
| 60 | + renameFile(tableLocation, tableFiles.get(1), '_' + tableFiles.get(1)); |
| 61 | + |
| 62 | + assertThat(onTrino().executeQuery("SELECT * FROM " + tableName)).hasNoRows(); |
| 63 | + assertThat(onHive().executeQuery("SELECT * FROM " + tableName)).hasNoRows(); |
| 64 | + |
| 65 | + onTrino().executeQuery("DROP TABLE IF EXISTS " + tableName); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testSelectFromTableContainingFilenamesWithUnderscore() |
| 70 | + throws Exception |
| 71 | + { |
| 72 | + String tableName = "test_table_visible_underscore_files" + randomNameSuffix(); |
| 73 | + onTrino().executeQuery("CREATE TABLE " + tableName + " AS SELECT 1 AS col"); |
| 74 | + |
| 75 | + List<QueryAssert.Row> tableRows = List.of(row(1)); |
| 76 | + assertThat(onTrino().executeQuery("SELECT * FROM " + tableName)).containsOnly(tableRows); |
| 77 | + assertThat(onHive().executeQuery("SELECT * FROM " + tableName)).containsOnly(tableRows); |
| 78 | + |
| 79 | + String tableLocation = getTablePath(tableName); |
| 80 | + // Prefix the table files with `f_` which should still keep them visible to Hive |
| 81 | + for (String filename : hdfsClient.listDirectory(tableLocation)) { |
| 82 | + // As long as the file is not hidden (starting with `.` or `_`), it should not be ignored by Hive |
| 83 | + renameFile(tableLocation, filename, "f_" + filename); |
| 84 | + } |
| 85 | + |
| 86 | + assertThat(onTrino().executeQuery("SELECT * FROM " + tableName)).containsOnly(tableRows); |
| 87 | + assertThat(onHive().executeQuery("SELECT * FROM " + tableName)).containsOnly(tableRows); |
| 88 | + |
| 89 | + onTrino().executeQuery("DROP TABLE " + tableName); |
| 90 | + } |
| 91 | + |
| 92 | + private void renameFile(String directoryLocation, String filename, String newFilename) |
| 93 | + throws IOException |
| 94 | + { |
| 95 | + try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { |
| 96 | + hdfsClient.loadFile(directoryLocation + "/" + filename, bos); |
| 97 | + hdfsClient.saveFile(directoryLocation + "/" + newFilename, new ByteArrayInputStream(bos.toByteArray())); |
| 98 | + hdfsClient.delete(directoryLocation + "/" + filename); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments