GitHub user HyukjinKwon opened a pull request:

    https://github.com/apache/spark/pull/17310

    [SPARK-18579][SQL] Use ignoreLeadingWhiteSpace and ignoreTrailingWhiteSpace 
options in CSV writing

    ## What changes were proposed in this pull request?
    
    This PR proposes to support _not_ trimming the white spaces. These are 
`false` by default in CSV reading path but these are `true` by default in CSV 
writing in univocity parser.
    
    Both `ignoreLeadingWhiteSpace` and `ignoreTrailingWhiteSpace` options are 
not being used for writing and therefore, we are always trimming the white 
spaces.
    
    It seems we should provide a way to keep this white spaces easily.
    
    WIth the data below:
    
    
    ```scala
    val df = spark.read.csv(Seq("a , b  , c").toDS)
    df.show()
    ```
    
    ```
    +---+----+---+
    |_c0| _c1|_c2|
    +---+----+---+
    | a | b  |  c|
    +---+----+---+
    ```
    
    **Before**
    
    ```scala
    df.write.csv("/tmp/text.csv")
    spark.read.text("/tmp/text.csv").show()
    ```
    
    ```
    +-----+
    |value|
    +-----+
    |a,b,c|
    +-----+
    ```
    
    It seems this can't be worked around via `quoteAll` too.
    
    ```scala
    df.write.option("quoteAll", true).csv("/tmp/text.csv")
    spark.read.text("/tmp/text.csv").show()
    ```
    ```
    +-----------+
    |      value|
    +-----------+
    |"a","b","c"|
    +-----------+
    ```
    
    **After**
    
    
    ```scala
    df.write.option("ignoreLeadingWhiteSpace", 
false).option("ignoreTrailingWhiteSpace", false).csv("/tmp/text.csv")
    spark.read.text("/tmp/text.csv").show()
    ```
    
    ```
    +----------+
    |     value|
    +----------+
    |a , b  , c|
    +----------+
    ```
    
    Note that this case is possible in R
    
    ```r
    > system("cat text.csv")
    f1,f2,f3
    a , b  , c
    > df <- read.csv(file="text.csv")
    > df
      f1   f2 f3
    1 a   b    c
    > write.csv(df, file = "text1.csv", quote=F, row.names=F)
    > system("cat text1.csv")
    f1,f2,f3
    a , b  , c
    ```
    
    ## How was this patch tested?
    
    Unit tests in `CSVSuite` and manual tests for Python.

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/HyukjinKwon/spark SPARK-18579

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/spark/pull/17310.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #17310
    
----
commit a16f0c5158b84a63ab1a5e89562cd664e161077a
Author: hyukjinkwon <[email protected]>
Date:   2017-03-16T06:58:14Z

    Use ignoreLeadingWhiteSpace and ignoreTrailingWhiteSpace options in CSV 
writing

----


---
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 [email protected] or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to