[SPARK-58518][SQL] Do not duplicate input paths when globbing is disabled - #57769
Open
Joorgem wants to merge 1 commit into
Open
[SPARK-58518][SQL] Do not duplicate input paths when globbing is disabled#57769Joorgem wants to merge 1 commit into
Joorgem wants to merge 1 commit into
Conversation
…bled checkAndGlobPathIfNecessary runs its lambda once per glob-looking path, but the enableGlobbing == false branch returned qualifiedPaths -- the whole input list. With G glob-looking paths and n others the result held G*(G+n)+n entries instead of G+n. Those duplicates become the file index rootPaths and, for an unpartitioned relation, reach FileScanRDD, so the same file is read repeatedly and the query returns duplicate rows. The branch now returns the path it was handed. For a single path, which is every case SPARK-32810 introduced the option for and tested, the two expressions are the same value. The highest-impact caller is the streaming file source, which disables globbing unconditionally, so a microbatch containing one file whose name holds a glob metacharacter emits every other file in that batch more than once.
uros-b
approved these changes
Aug 4, 2026
Member
|
Thank you @Joorgem! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changes were proposed in this pull request?
DataSource.checkAndGlobPathIfNecessaryruns its lambda once per glob-looking path, but theenableGlobbing == falsebranch returnedqualifiedPaths— the whole input list:The branch now returns
Seq(globPath), the path it was handed.Why are the changes needed?
The query returns duplicate rows. With
Gpaths that look like globs andnthat do not, the result heldG*(G+n)+nentries instead ofG+n. Those duplicates become the file index'srootPaths, and for an unpartitioned relationPartitioningAwareFileIndex.allFiles()doesrootPaths.flatMap, so the sameFileStatusis returned several times andFileScanRDDreads the same file repeatedly.Three single-row files, one of them named
weird_[x].csv:"Looks like a glob" is a bare character scan with no notion of escaping (
SparkHadoopUtil.isGlobPathtests for any of{}[]*?\), so the branch is reached by ordinary data. Filenames containing brackets are legal on HDFS and S3 — and SPARK-32810 and SPARK-32815 exist precisely because the project decided such names must work.The highest-impact caller is the Structured Streaming file source, which sets
GLOB_PATHS_KEY -> "false"unconditionally and hands the whole microbatch's file list to aDataSource. So a microbatch containing one file whose name holds a metacharacter emits every other file in that batch more than once.MLUtils.parseLibSVMFileand theTextInput*.inferschema-inference paths pass multiple paths with globbing disabled too.The diff is one expression, so it is worth being explicit that the finding is silent data duplication rather than a style change.
Does this PR introduce any user-facing change?
Yes, and only in the direction of correctness: affected reads return the right number of rows instead of a larger one, and do less I/O.
No behaviour changes for the case the option was introduced to serve. With a single path,
qualifiedPathsandSeq(globPath)are the same value, so every scenario SPARK-32810 fixed and tested behaves identically.globResultstays non-empty, so thecheckEmptyGlobPathcheck cannot begin raisingPATH_NOT_FOUNDwhere it did not before.How was this patch tested?
Two tests added to
DataSourceSuite, and verified to fail on unmodifiedmasterbefore the fix was applied — the fork CI run of the test alone reportedTests: succeeded 21345, failed 2, the two failures being exactly these, with the duplication visible in the messages:With the fix, the full matrix is green.
Both tests assert on ordered sequences rather than sets, and that is deliberate. Every existing assertion in this suite compares
resultPaths.toSetagainst aSet— and aSetis exactly what erases duplication. Together with the fact that all six existingcheckAndGlobPathIfNecessarytests passenableGlobbing = true, that is why this went unnoticed: the branch was never exercised, and the suite's assertion style could not have caught it if it had been.The end-to-end test uses
textrather than CSV, since the defect is inDataSourceand is format-agnostic. Verified by hand intext,json,csvandparquet, all returning five rows for three files.The predicted count holds exactly, which is what distinguishes a diagnosis from an observation:
G*(G+n)+nThe
G=1, n=0row is also the answer to why this survived since 2020: every test SPARK-32810 added reads a single path, and that is the one case that comes back correct.Scope, stated honestly: the correctness impact needs an unpartitioned relation. When partition columns are discovered,
allFiles()takes a branch returning aMap's values, which absorbs the duplicates — measured on the same data, 15 rows withoutbasePathagainst 9 with it. On partitioned relations this remains wasted listing work rather than wrong results.Was this patch authored or co-authored using generative AI tooling?
Generated-by: Claude Code (Opus 5)
Related
pathproperty repeating the table LOCATION, and was fixed in [SPARK-28266][SQL] convertToLogicalRelation should not interpretpathproperty when reading Hive tables #33328 — nowhere near this method. It does establish that duplicated input paths are treated as a correctness bug.enableGlobbingapart from [SPARK-32810][SQL] CSV/JSON data sources should avoid globbing paths when inferring schema #29659.