GitHub user HyukjinKwon opened a pull request:

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

    [SPARK-16896][SQL] Handle duplicated field names in header consistently 
with null or empty strings in CSV

    ## What changes were proposed in this pull request?
    
    Currently, CSV datasource allows to load duplicated empty string fields or 
fields having `nullValue` in the header. It'd be great if this can deal with 
normal fields as well.
    
    BTW, in the JIRA, there is a good reference, `read.csv()` in R. So, I 
initially wanted to propose the similar behaviour.
    
    In case of R,  the CSV data below:
    
    ```
    fieldA,fieldB,fieldA,fieldA
    1,2,3,4
    ```
    
    is parsed as below:
    
    ```r
    test <- read.csv(file="test.csv",head=TRUE,sep=",")
    > test
      fieldA fieldB X fieldA.1 fieldA.2 X.1 X.2
    1      1      2 3        4        5   6   7
    ```
    
    However, Spark CSV datasource already is handling duplicated empty strings 
and `nullValue` as field names. So the data below:
    
    ```
    ,,,fieldA,,fieldB,
    1,2,3,4,5,6,7
    ```
    
    is parsed as below:
    
    ```scala
    spark.read.format("csv").option("header", "true").load("test.csv").show()
    ```
    ```
    +---+---+---+------+---+------+---+
    |_c0|_c1|_c2|fieldA|_c4|fieldB|_c6|
    +---+---+---+------+---+------+---+
    |  1|  2|  3|     4|  5|     6|  7|
    +---+---+---+------+---+------+---+
    ```
    
    To cut this short,
    
    Here, R adds the number for each duplicate but Spark adds the number for 
its position for all fields. Therefore, this PR proposes handling the 
duplicates consistently with the existing behaviour as below:
    
    data below:
    
    ```
    fieldA,fieldB,fieldA,fieldA
    1,2,3,4
    ```
    
    is parsed as below:
    
    ```scala
    spark.read.format("csv").option("header", "true").load("test.csv").show()
    ```
    
    ```
    +-------+------+---+-------+-------+---+---+
    |fieldA0|fieldB|_c2|fieldA3|fieldA4|_c5|_c6|
    +-------+------+---+-------+-------+---+---+
    |      1|     2|  3|      4|      5|  6|  7|
    +-------+------+---+-------+-------+---+---+
    ```
    
    
    ## How was this patch tested?
    
    Unit test in `CSVSuite`.

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

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

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

    https://github.com/apache/spark/pull/14745.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 #14745
    
----
commit 0d31ba7da9c88dcf87ef711e077b37778651aa2b
Author: hyukjinkwon <gurwls...@gmail.com>
Date:   2016-08-22T04:17:44Z

    Load duplicated field names consistently with null or empty strings

commit a3c3dc6231429b4969baa800ba778ddf0f0aaf12
Author: hyukjinkwon <gurwls...@gmail.com>
Date:   2016-08-22T04:21:52Z

    Fix style nits

----


---
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

Reply via email to