-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-26081][SQL] Prevent empty files for empty partitions in Text datasources #23052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3efa7b6
80aadf6
0a774ef
47b71b7
040c71f
6f3cb18
9501d01
76e1466
167fc4d
586ab31
ed93caa
129b393
083d411
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -169,13 +169,19 @@ private[csv] class CsvOutputWriter( | |
| context: TaskAttemptContext, | ||
| params: CSVOptions) extends OutputWriter with Logging { | ||
|
|
||
| private val charset = Charset.forName(params.charset) | ||
|
|
||
| private val writer = CodecStreams.createOutputStreamWriter(context, new Path(path), charset) | ||
|
|
||
| private val gen = new UnivocityGenerator(dataSchema, writer, params) | ||
| private var univocityGenerator: Option[UnivocityGenerator] = None | ||
|
|
||
| override def write(row: InternalRow): Unit = { | ||
| val gen = univocityGenerator.getOrElse { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, one thing we should not forget about is, CSV could have headers even if the records are empty.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do think it is fine to write only headers if an user wants to have them. Filtering the header out on this level could be slightly difficult. |
||
| val charset = Charset.forName(params.charset) | ||
| val os = CodecStreams.createOutputStreamWriter(context, new Path(path), charset) | ||
| val newGen = new UnivocityGenerator(dataSchema, os, params) | ||
| univocityGenerator = Some(newGen) | ||
| newGen | ||
| } | ||
|
|
||
| override def write(row: InternalRow): Unit = gen.write(row) | ||
| gen.write(row) | ||
| } | ||
|
|
||
| override def close(): Unit = gen.close() | ||
| override def close(): Unit = univocityGenerator.map(_.close()) | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we have a race condition below then where multiple generators can be created?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have not observe any race conditions so far. Instances of
UnivocityGeneratorare created per-tasks as well asOutputStreamWriters. They share instances of schema and CSVOptions but we do not modify them while writing. Inside of eachUnivocityGenerator, we create an instance ofCsvWriterbut I almost absolutely sure they do not share anything internally.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't mean that it would cause an error, but that it could create many generators and writers that aren't closed. It may not be obvious that it's happening. Unless we know writes will only happen in one thread what about breaking out and synchronizing the get/create part of this method?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Writers/generators are created inside of tasks:
spark/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatWriter.scala
Lines 228 to 256 in ab1650d
dataWriter.commit()anddataWriter.abort()close writers/generators. So, number of not closed generators is less or equal to the size of the task thread pool on executors at any moment.According to comments below, this is our assumption:
spark/sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/FileFormatDataWriter.scala
Lines 33 to 37 in e816776