Repository: duckdb/duckdb
File: src/common/virtual_file_system.cpp (C++)

Code before:
		}
		if (IsFileCompressed(file.path, FileCompressionType::GZIP)) {
			compression = FileCompressionType::GZIP;
		} else if (IsFileCompressed(file.path, FileCompressionType::ZSTD)) {
			compression = FileCompressionType::ZSTD;
		} else {
			compression = FileCompressionType::UNCOMPRESSED;
		}
	}

	// open the base file handle in UNCOMPRESSED mode
	flags.SetCompression(FileCompressionType::UNCOMPRESSED);

	auto &internal_filesystem = FindFileSystem(file.path, opener);

	// File handle gets created.
	unique_ptr<FileHandle> file_handle = nullptr;

	// Handle caching logic.
	if (flags.GetCachingMode() != CachingMode::NO_CACHING) {
		auto caching_filesystem =
		    make_shared_ptr<CachingFileSystemWrapper>(internal_filesystem, opener, flags.GetCachingMode());
		file_handle = caching_filesystem->OpenFile(file, flags, opener);
		// CachingFileSystemWrapper is not stored within VFS, so pin its lifecycle inside of file handle.
		auto &caching_file_handle = file_handle->Cast<CachingFileHandleWrapper>();
		caching_file_handle.PinCachingFileSystem(std::move(caching_filesystem));
	} else {
		file_handle = internal_filesystem.OpenFile(file, flags, opener);
	}
	if (!file_handle) {
		return nullptr;
	}

	// Evaluate and apply compression option then.
	const auto context = !flags.MultiClientAccess() ? FileOpener::TryGetClientContext(opener) : QueryContext();
	if (file_handle->GetType() == FileType::FILE_TYPE_FIFO) {
		file_handle = PipeFileSystem::OpenPipe(context, std::move(file_handle));
	} else if (compression != FileCompressionType::UNCOMPRESSED) {
		auto entry = compressed_fs.find(compression);
		if (entry == compressed_fs.end()) {
			if (compression == FileCompressionType::ZSTD) {
				throw NotImplementedException(
				    "Attempting to open a compressed file, but the compression type is not supported.\nConsider "
				    "explicitly \"INSTALL parquet; LOAD parquet;\" to support this compression scheme");
			}
			throw NotImplementedException(
			    "Attempting to open a compressed file, but the compression type is not supported");
		}
		file_handle = entry->second->OpenCompressedFile(context, std::move(file_handle), flags.OpenForWriting());
	}
	return file_handle;

Review comment (refactor):
Could we move this logic into `CachingFileSystemWrapper` instead of in `VirtualFileSystem`?

Code after:
		}
		if (IsFileCompressed(file.path, FileCompressionType::GZIP)) {
			compression = FileCompressionType::GZIP;
		} else if (IsFileCompressed(file.path, FileCompressionType::ZSTD)) {
			compression = FileCompressionType::ZSTD;
		} else {
			compression = FileCompressionType::UNCOMPRESSED;
		}
	}

	// open the base file handle in UNCOMPRESSED mode
	flags.SetCompression(FileCompressionType::UNCOMPRESSED);

	auto &internal_filesystem = FindFileSystem(file.path, opener);

	// File handle gets created.
	unique_ptr<FileHandle> file_handle = nullptr;

	// Handle caching logic.
	if (flags.GetCachingMode() != CachingMode::NO_CACHING) {
		auto caching_filesystem =
		    make_shared_ptr<CachingFileSystemWrapper>(internal_filesystem, opener, flags.GetCachingMode());
		// caching filesystem's lifecycle is extended inside of caching file handle.
		file_handle = caching_filesystem->OpenFile(file, flags, opener);
	} else {
		file_handle = internal_filesystem.OpenFile(file, flags, opener);
	}
	if (!file_handle) {
		return nullptr;
	}

	// Evaluate and apply compression option then.
	const auto context = !flags.MultiClientAccess() ? FileOpener::TryGetClientContext(opener) : QueryContext();
	if (file_handle->GetType() == FileType::FILE_TYPE_FIFO) {
		file_handle = PipeFileSystem::OpenPipe(context, std::move(file_handle));
	} else if (compression != FileCompressionType::UNCOMPRESSED) {
		auto entry = compressed_fs.find(compression);
		if (entry == compressed_fs.end()) {
			if (compression == FileCompressionType::ZSTD) {
				throw NotImplementedException(
				    "Attempting to open a compressed file, but the compression type is not supported.\nConsider "
				    "explicitly \"INSTALL parquet; LOAD parquet;\" to support this compression scheme");
			}
			throw NotImplementedException(
			    "Attempting to open a compressed file, but the compression type is not supported");
		}
		file_handle = entry->second->OpenCompressedFile(context, std::move(file_handle), flags.OpenForWriting());
	}
	return file_handle;
}