Github user andreweduffy commented on a diff in the pull request: https://github.com/apache/spark/pull/15835#discussion_r87499298 --- Diff: sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/parquet/ParquetFileFormat.scala --- @@ -272,6 +275,83 @@ class ParquetFileFormat true } + override def getSplits(fileIndex: FileIndex, + fileStatus: FileStatus, + filters: Seq[Filter], + schema: StructType, + hadoopConf: Configuration): Seq[FileSplit] = { + if (filters.isEmpty) { + // Return immediately to save FileSystem overhead + super.getSplits(fileIndex, fileStatus, filters, schema, hadoopConf) + } else { + val filePath = fileStatus.getPath + val rootOption: Option[Path] = fileIndex.rootPaths + .find(root => filePath.toString.startsWith(root.toString)) + val metadataOption = rootOption.flatMap(getMetadataForPath(filePath, _, hadoopConf)) + // If the metadata exists, filter the splits. + // Otherwise, fall back to the default implementation. + metadataOption + .map(filterToSplits(fileStatus, _, rootOption.get, filters, schema, hadoopConf)) + .getOrElse(super.getSplits(fileIndex, fileStatus, filters, schema, hadoopConf)) + } + } + + private def filterToSplits(fileStatus: FileStatus, + metadata: ParquetMetadata, + metadataRoot: Path, + filters: Seq[Filter], + schema: StructType, + hadoopConf: Configuration): Seq[FileSplit] = { + val metadataBlocks = metadata.getBlocks + val parquetSchema = metadata.getFileMetaData.getSchema + val filter = FilterCompat.get(filters + .flatMap(ParquetFilters.createFilter(schema, _)) + .reduce(FilterApi.and)) + val filteredMetadata = + RowGroupFilter.filterRowGroups(filter, metadataBlocks, parquetSchema).asScala + filteredMetadata.flatMap(bmd => { + val bmdPath = new Path(metadataRoot, bmd.getPath) + val fsPath = fileStatus.getPath + if (bmdPath == fsPath) { + Some(new FileSplit(bmdPath, bmd.getStartingPos, bmd.getTotalByteSize, Array.empty)) + } else { + None + } + }) + } + + private def getMetadataForPath(filePath: Path, + rootPath: Path, + conf: Configuration): Option[ParquetMetadata] = { + val fs = rootPath.getFileSystem(conf) + try { + val stat = fs.getFileStatus(rootPath) + // Mimic Parquet behavior. If given a directory, find the underlying _metadata file + // If given a single file, check the parent directory for a _metadata file + val directory = if (stat.isDirectory) stat.getPath else stat.getPath.getParent + val metadataFile = new Path(directory, ParquetFileWriter.PARQUET_METADATA_FILE) + val metadata = + ParquetFileReader.readFooter(conf, metadataFile, ParquetMetadataConverter.NO_FILTER) + + // Ensure that the metadata has an entry for the file. + // If it does not, do not filter at this stage. + val matchingBlockExists = metadata.getBlocks.asScala.exists(bmd => { --- End diff -- I'm a little unclear as to why this branch is necessary. Under what conditions do you have a parquet dataset where one of its files isn't in the summary metadata? Is this the case where you write out the dataset with metadata, then add a new partition, and write without updating the metadata?
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org