Skip to content

Commit 7d0c344

Browse files
pettyjamesmarhimondr
authored andcommitted
Add Hive FileSystem listing test
1 parent f0cdc02 commit 7d0c344

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

plugin/trino-hive/src/test/java/io/trino/plugin/hive/AbstractTestHiveFileSystem.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
import com.google.common.collect.ImmutableList;
1717
import com.google.common.collect.ImmutableSet;
18+
import com.google.common.collect.Iterators;
19+
import com.google.common.collect.Lists;
1820
import com.google.common.net.HostAndPort;
1921
import io.airlift.concurrent.BoundedExecutor;
2022
import io.airlift.json.JsonCodec;
@@ -28,13 +30,15 @@
2830
import io.trino.plugin.hive.authentication.HiveIdentity;
2931
import io.trino.plugin.hive.authentication.NoHdfsAuthentication;
3032
import io.trino.plugin.hive.fs.FileSystemDirectoryLister;
33+
import io.trino.plugin.hive.fs.HiveFileIterator;
3134
import io.trino.plugin.hive.metastore.Column;
3235
import io.trino.plugin.hive.metastore.Database;
3336
import io.trino.plugin.hive.metastore.ForwardingHiveMetastore;
3437
import io.trino.plugin.hive.metastore.HiveMetastore;
3538
import io.trino.plugin.hive.metastore.HiveMetastoreConfig;
3639
import io.trino.plugin.hive.metastore.HiveMetastoreFactory;
3740
import io.trino.plugin.hive.metastore.PrincipalPrivileges;
41+
import io.trino.plugin.hive.metastore.StorageFormat;
3842
import io.trino.plugin.hive.metastore.Table;
3943
import io.trino.plugin.hive.metastore.thrift.BridgingHiveMetastore;
4044
import io.trino.plugin.hive.metastore.thrift.MetastoreLocator;
@@ -69,6 +73,7 @@
6973
import io.trino.testing.TestingNodeManager;
7074
import io.trino.type.BlockTypeOperators;
7175
import org.apache.hadoop.fs.FileSystem;
76+
import org.apache.hadoop.fs.LocatedFileStatus;
7277
import org.apache.hadoop.fs.Path;
7378
import org.apache.hadoop.fs.azurebfs.AzureBlobFileSystem;
7479
import org.testng.annotations.AfterClass;
@@ -102,6 +107,7 @@
102107
import static io.trino.plugin.hive.HiveTestUtils.getHiveSession;
103108
import static io.trino.plugin.hive.HiveTestUtils.getHiveSessionProperties;
104109
import static io.trino.plugin.hive.HiveTestUtils.getTypes;
110+
import static io.trino.plugin.hive.HiveType.HIVE_LONG;
105111
import static io.trino.plugin.hive.metastore.PrincipalPrivileges.NO_PRIVILEGES;
106112
import static io.trino.plugin.hive.util.HiveWriteUtils.getRawFileSystem;
107113
import static io.trino.spi.connector.MetadataProvider.NOOP_METADATA_PROVIDER;
@@ -400,6 +406,81 @@ public void testRename()
400406
fs.delete(basePath, true);
401407
}
402408

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+
403484
@Test
404485
public void testTableCreation()
405486
throws Exception

0 commit comments

Comments
 (0)