|
| 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.plugin.hive.fs; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | +import com.google.common.collect.ImmutableMap; |
| 18 | +import io.airlift.units.Duration; |
| 19 | +import io.trino.plugin.hive.metastore.MetastoreUtil; |
| 20 | +import io.trino.plugin.hive.metastore.Table; |
| 21 | +import io.trino.testing.QueryRunner; |
| 22 | +import org.apache.hadoop.fs.Path; |
| 23 | +import org.testng.annotations.Test; |
| 24 | + |
| 25 | +import java.util.List; |
| 26 | +import java.util.NoSuchElementException; |
| 27 | + |
| 28 | +import static io.trino.plugin.hive.HiveQueryRunner.TPCH_SCHEMA; |
| 29 | +import static io.trino.plugin.hive.metastore.PrincipalPrivileges.NO_PRIVILEGES; |
| 30 | +import static java.lang.String.format; |
| 31 | +import static org.testng.Assert.assertFalse; |
| 32 | +import static org.testng.Assert.assertTrue; |
| 33 | + |
| 34 | +// some tests may invalidate the whole cache affecting therefore other concurrent tests |
| 35 | +@Test(singleThreaded = true) |
| 36 | +public class TestCachingDirectoryListerRecursiveFilesOnly |
| 37 | + extends BaseCachingDirectoryListerTest<CachingDirectoryLister> |
| 38 | +{ |
| 39 | + @Override |
| 40 | + protected CachingDirectoryLister createDirectoryLister() |
| 41 | + { |
| 42 | + return new CachingDirectoryLister(Duration.valueOf("5m"), 1_000_000L, List.of("tpch.*")); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + protected QueryRunner createQueryRunner() |
| 47 | + throws Exception |
| 48 | + { |
| 49 | + return createQueryRunner(ImmutableMap.of( |
| 50 | + "hive.allow-register-partition-procedure", "true", |
| 51 | + "hive.recursive-directories", "true")); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected boolean isCached(CachingDirectoryLister directoryLister, Path path) |
| 56 | + { |
| 57 | + return directoryLister.isCached(new DirectoryListingCacheKey(path, true)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testRecursiveDirectories() |
| 62 | + { |
| 63 | + // Create partitioned table to force files to be inserted in sub-partition paths |
| 64 | + assertUpdate("CREATE TABLE recursive_directories (clicks bigint, day date, country varchar) WITH (format = 'ORC', partitioned_by = ARRAY['day', 'country'])"); |
| 65 | + assertUpdate("INSERT INTO recursive_directories VALUES (1000, DATE '2022-02-01', 'US'), (2000, DATE '2022-02-01', 'US'), (4000, DATE '2022-02-02', 'US'), (1500, DATE '2022-02-01', 'AT'), (2500, DATE '2022-02-02', 'AT')", 5); |
| 66 | + |
| 67 | + // Replace the partitioned table a new unpartitioned table with the same root location, leaving the data in place |
| 68 | + Table partitionedTable = getTable(TPCH_SCHEMA, "recursive_directories") |
| 69 | + .orElseThrow(() -> new NoSuchElementException(format("Failed to read table %s.%s", TPCH_SCHEMA, "recursive_directories"))); |
| 70 | + // Must not delete the data files when dropping the partitioned table |
| 71 | + dropTable(TPCH_SCHEMA, "recursive_directories", false); |
| 72 | + // Must create the table directly to bypass check that the target directory already exists |
| 73 | + Table testTable = Table.builder(partitionedTable) |
| 74 | + .setPartitionColumns(ImmutableList.of()) |
| 75 | + .build(); |
| 76 | + createTable(testTable, testTable.getOwner().map(MetastoreUtil::buildInitialPrivilegeSet).orElse(NO_PRIVILEGES)); |
| 77 | + |
| 78 | + // Execute a query on the new table to pull the listing into the cache |
| 79 | + assertQuery("SELECT sum(clicks) FROM recursive_directories", "VALUES (11000)"); |
| 80 | + |
| 81 | + Path tableLocation = getTableLocation(TPCH_SCHEMA, "recursive_directories"); |
| 82 | + assertTrue(isCached(tableLocation)); |
| 83 | + |
| 84 | + // Insert should invalidate cache, even at the root directory path |
| 85 | + assertUpdate("INSERT INTO recursive_directories VALUES (1000)", 1); |
| 86 | + assertFalse(isCached(tableLocation)); |
| 87 | + |
| 88 | + // Results should include the new insert which is at the table location root for the unpartitioned table |
| 89 | + assertQuery("SELECT sum(clicks) FROM recursive_directories", "VALUES (12000)"); |
| 90 | + |
| 91 | + assertUpdate("DROP TABLE recursive_directories"); |
| 92 | + |
| 93 | + assertFalse(isCached(tableLocation)); |
| 94 | + } |
| 95 | +} |
0 commit comments