|
15 | 15 |
|
16 | 16 | import com.google.common.collect.ImmutableList; |
17 | 17 | import com.google.common.collect.ImmutableSet; |
| 18 | +import com.google.common.collect.Iterators; |
| 19 | +import com.google.common.collect.Lists; |
18 | 20 | import com.google.common.net.HostAndPort; |
19 | 21 | import io.airlift.concurrent.BoundedExecutor; |
20 | 22 | import io.airlift.json.JsonCodec; |
|
28 | 30 | import io.trino.plugin.hive.authentication.HiveIdentity; |
29 | 31 | import io.trino.plugin.hive.authentication.NoHdfsAuthentication; |
30 | 32 | import io.trino.plugin.hive.fs.FileSystemDirectoryLister; |
| 33 | +import io.trino.plugin.hive.fs.HiveFileIterator; |
31 | 34 | import io.trino.plugin.hive.metastore.Column; |
32 | 35 | import io.trino.plugin.hive.metastore.Database; |
33 | 36 | import io.trino.plugin.hive.metastore.ForwardingHiveMetastore; |
34 | 37 | import io.trino.plugin.hive.metastore.HiveMetastore; |
35 | 38 | import io.trino.plugin.hive.metastore.HiveMetastoreConfig; |
36 | 39 | import io.trino.plugin.hive.metastore.HiveMetastoreFactory; |
37 | 40 | import io.trino.plugin.hive.metastore.PrincipalPrivileges; |
| 41 | +import io.trino.plugin.hive.metastore.StorageFormat; |
38 | 42 | import io.trino.plugin.hive.metastore.Table; |
39 | 43 | import io.trino.plugin.hive.metastore.thrift.BridgingHiveMetastore; |
40 | 44 | import io.trino.plugin.hive.metastore.thrift.MetastoreLocator; |
|
69 | 73 | import io.trino.testing.TestingNodeManager; |
70 | 74 | import io.trino.type.BlockTypeOperators; |
71 | 75 | import org.apache.hadoop.fs.FileSystem; |
| 76 | +import org.apache.hadoop.fs.LocatedFileStatus; |
72 | 77 | import org.apache.hadoop.fs.Path; |
73 | 78 | import org.apache.hadoop.fs.azurebfs.AzureBlobFileSystem; |
74 | 79 | import org.testng.annotations.AfterClass; |
|
102 | 107 | import static io.trino.plugin.hive.HiveTestUtils.getHiveSession; |
103 | 108 | import static io.trino.plugin.hive.HiveTestUtils.getHiveSessionProperties; |
104 | 109 | import static io.trino.plugin.hive.HiveTestUtils.getTypes; |
| 110 | +import static io.trino.plugin.hive.HiveType.HIVE_LONG; |
105 | 111 | import static io.trino.plugin.hive.metastore.PrincipalPrivileges.NO_PRIVILEGES; |
106 | 112 | import static io.trino.plugin.hive.util.HiveWriteUtils.getRawFileSystem; |
107 | 113 | import static io.trino.spi.connector.MetadataProvider.NOOP_METADATA_PROVIDER; |
@@ -400,6 +406,81 @@ public void testRename() |
400 | 406 | fs.delete(basePath, true); |
401 | 407 | } |
402 | 408 |
|
| 409 | + @Test |
| 410 | + public void testFileIteratorListing() |
| 411 | + throws Exception |
| 412 | + { |
| 413 | + Table.Builder tableBuilder = Table.builder() |
| 414 | + .setDatabaseName(table.getSchemaName()) |
| 415 | + .setTableName(table.getTableName()) |
| 416 | + .setDataColumns(ImmutableList.of(new Column("one", HIVE_LONG, Optional.empty()))) |
| 417 | + .setPartitionColumns(ImmutableList.of()) |
| 418 | + .setOwner(Optional.empty()) |
| 419 | + .setTableType("fake"); |
| 420 | + tableBuilder.getStorageBuilder() |
| 421 | + .setStorageFormat(StorageFormat.fromHiveStorageFormat(HiveStorageFormat.CSV)); |
| 422 | + Table fakeTable = tableBuilder.build(); |
| 423 | + |
| 424 | + // Expected file system tree: |
| 425 | + // test-file-iterator-listing/ |
| 426 | + // .hidden/ |
| 427 | + // nested-file-in-hidden.txt |
| 428 | + // parent/ |
| 429 | + // _nested-hidden-file.txt |
| 430 | + // nested-file.txt |
| 431 | + // empty-directory/ |
| 432 | + // .hidden-in-base.txt |
| 433 | + // base-path-file.txt |
| 434 | + Path basePath = new Path(getBasePath(), "test-file-iterator-listing"); |
| 435 | + FileSystem fs = hdfsEnvironment.getFileSystem(TESTING_CONTEXT, basePath); |
| 436 | + fs.mkdirs(basePath); |
| 437 | + |
| 438 | + // create file in hidden folder |
| 439 | + Path fileInHiddenParent = new Path(new Path(basePath, ".hidden"), "nested-file-in-hidden.txt"); |
| 440 | + fs.createNewFile(fileInHiddenParent); |
| 441 | + // create hidden file in non-hidden folder |
| 442 | + Path nestedHiddenFile = new Path(new Path(basePath, "parent"), "_nested-hidden-file.txt"); |
| 443 | + fs.createNewFile(nestedHiddenFile); |
| 444 | + // create file in non-hidden folder |
| 445 | + Path nestedFile = new Path(new Path(basePath, "parent"), "nested-file.txt"); |
| 446 | + fs.createNewFile(nestedFile); |
| 447 | + // create file in base path |
| 448 | + Path baseFile = new Path(basePath, "base-path-file.txt"); |
| 449 | + fs.createNewFile(baseFile); |
| 450 | + // create hidden file in base path |
| 451 | + Path hiddenBase = new Path(basePath, ".hidden-in-base.txt"); |
| 452 | + fs.createNewFile(hiddenBase); |
| 453 | + // create empty subdirectory |
| 454 | + Path emptyDirectory = new Path(basePath, "empty-directory"); |
| 455 | + fs.mkdirs(emptyDirectory); |
| 456 | + |
| 457 | + // List recursively through hive file iterator |
| 458 | + HiveFileIterator recursiveIterator = new HiveFileIterator( |
| 459 | + fakeTable, |
| 460 | + basePath, |
| 461 | + fs, |
| 462 | + new FileSystemDirectoryLister(), |
| 463 | + new NamenodeStats(), |
| 464 | + HiveFileIterator.NestedDirectoryPolicy.RECURSE, |
| 465 | + false); // ignoreAbsentPartitions |
| 466 | + |
| 467 | + List<Path> recursiveListing = Lists.newArrayList(Iterators.transform(recursiveIterator, LocatedFileStatus::getPath)); |
| 468 | + // Should not include directories, or files underneath hidden directories |
| 469 | + assertEqualsIgnoreOrder(recursiveListing, ImmutableList.of(nestedFile, baseFile)); |
| 470 | + |
| 471 | + HiveFileIterator shallowIterator = new HiveFileIterator( |
| 472 | + fakeTable, |
| 473 | + basePath, |
| 474 | + fs, |
| 475 | + new FileSystemDirectoryLister(), |
| 476 | + new NamenodeStats(), |
| 477 | + HiveFileIterator.NestedDirectoryPolicy.IGNORED, |
| 478 | + false); // ignoreAbsentPartitions |
| 479 | + List<Path> shallowListing = Lists.newArrayList(Iterators.transform(shallowIterator, LocatedFileStatus::getPath)); |
| 480 | + // Should not include any hidden files, folders, or nested files |
| 481 | + assertEqualsIgnoreOrder(shallowListing, ImmutableList.of(baseFile)); |
| 482 | + } |
| 483 | + |
403 | 484 | @Test |
404 | 485 | public void testTableCreation() |
405 | 486 | throws Exception |
|
0 commit comments