-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-21289][SQL][ML] Supports custom line separator for all text-based datasources #18581
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
Closed
Closed
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fb589a3
Adds lineSep option for all text-based datasources
HyukjinKwon c4282bf
Remove perentheses in the tests
HyukjinKwon e7983d6
Make tests as functions so that other lineSep cna be tested easily.
HyukjinKwon 16cc0c2
Minor cleanup
HyukjinKwon b006d65
`\n` by default, covering `\r`, `\r\n` and `\n`.
HyukjinKwon 5ce9895
Fix libsvm and text too
HyukjinKwon bc65e6b
Add parameter descriptions in HadoopFileLinesReader
HyukjinKwon a240973
Minor cleanup
HyukjinKwon 265dd48
Address comments
HyukjinKwon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -184,4 +184,62 @@ class LibSVMRelationSuite extends SparkFunSuite with MLlibTestSparkContext { | |
| spark.sql("DROP TABLE IF EXISTS libsvmTable") | ||
| } | ||
| } | ||
|
|
||
| def testLineSeparator(lineSep: String): Unit = { | ||
| test(s"SPARK-21289: Support line separator - lineSep: '$lineSep'") { | ||
| val data = Seq( | ||
| "1.0 1:1.0 3:2.0 5:3.0", "0.0", "0.0", "0.0 2:4.0 4:5.0 6:6.0").mkString(lineSep) | ||
| val dataWithTrailingLineSep = s"$data$lineSep" | ||
|
|
||
| Seq(data, dataWithTrailingLineSep).foreach { lines => | ||
| val path0 = new File(tempDir.getCanonicalPath, "write0") | ||
| val path1 = new File(tempDir.getCanonicalPath, "write1") | ||
| try { | ||
| // Read | ||
| java.nio.file.Files.write(path0.toPath, lines.getBytes(StandardCharsets.UTF_8)) | ||
|
Contributor
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. Why not import this ?
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. To differentiate it from google's |
||
| val df = spark.read | ||
| .option("lineSep", lineSep) | ||
| .format("libsvm") | ||
| .load(path0.getAbsolutePath) | ||
|
|
||
| assert(df.columns(0) == "label") | ||
| assert(df.columns(1) == "features") | ||
|
|
||
| val results = df.collect() | ||
|
|
||
| assert(results.map(_.getDouble(0)).toSet == Seq(1.0, 0.0, 0.0, 0.0).toSet) | ||
|
|
||
| val actual = results.map(_.getAs[SparseVector](1)) | ||
| val expected = Seq( | ||
| Vectors.sparse(6, Seq((0, 1.0), (2, 2.0), (4, 3.0))), | ||
| Vectors.sparse(6, Nil), | ||
| Vectors.sparse(6, Nil), | ||
| Vectors.sparse(6, Seq((1, 4.0), (3, 5.0), (5, 6.0)))) | ||
| assert(actual.toSet == expected.toSet) | ||
|
|
||
| // Write | ||
| df.coalesce(1) | ||
| .write.option("lineSep", lineSep).format("libsvm").save(path1.getAbsolutePath) | ||
| val partFile = Utils.recursiveList(path1).filter(f => f.getName.startsWith("part-")).head | ||
| val readBack = new String( | ||
| java.nio.file.Files.readAllBytes(partFile.toPath), StandardCharsets.UTF_8) | ||
| assert(readBack == dataWithTrailingLineSep) | ||
|
|
||
| // Roundtrip | ||
| val readBackDF = spark.read | ||
| .option("lineSep", lineSep) | ||
| .format("libsvm") | ||
| .load(path1.getAbsolutePath) | ||
| assert(df.collect().toSet == readBackDF.collect().toSet) | ||
| } finally { | ||
| Utils.deleteRecursively(path0) | ||
| Utils.deleteRecursively(path1) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Seq("123!!@", "^", "&@").foreach { lineSep => | ||
| testLineSeparator(lineSep) | ||
| } | ||
| } | ||
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
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
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
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
Oops, something went wrong.
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.
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.
Please use the full name.
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.
This name came after
sepin CSV which resembled R. Do you preferseparatorandlineSeparator?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.
Either
lineDelimiterorlineSeparatorlooks fine.In the future, we could also support field delimiters.
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 actually meant
sephere:spark/sql/core/src/main/scala/org/apache/spark/sql/DataFrameReader.scala
Lines 488 to 489 in 3c0c2d0
and was thinking of matching the name ..
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.
Just for history, it was
delimiterbut renamed tosep.