Strange behavior on CSV parser of Spark 2 when multiLine option is enabled

558 Views Asked by At

When creating a DataFrame from a CSV file, if multiLine option is enabled, some file columns are parsed incorrectly.

Here goes the code execution. I'll try to show the strange behaviors as the code goes.

First, I load the file in two variables: df_ok loads the file without multiLine option, while df_ko loads the file with multiLine option enabled. The file I'm using as example has \r\n as EOL, it is encoded in UTF-8 and has pipe as column separator.

val df_ok = spark.read.format("csv").option("header", "true").option("delimiter", "|").load("/.../20180423_LSV.csv")
val df_ko = spark.read.format("csv").option("header", "true").option("delimiter", "|").option("multiLine", "true").load("/.../20180423_LSV.csv")

df_ok.printSchema
root
 |-- FILIALE: string (nullable = true)
 |-- IDMDB: string (nullable = true)
 |-- FIMAGIC: string (nullable = true)
 ...
 |-- STATUS: string (nullable = true)
 |-- LSV_TYPE: string (nullable = true)

df_ko.printSchema
root
 |-- FILIALE: string (nullable = true)
 |-- IDMDB: string (nullable = true)
 |-- FIMAGIC: string (nullable = true)
 ...
 |-- STATUS: string (nullable = true)
 : string (nullable = true)

 df_ko.columns
 res0: Array[String] = Array(FILIALE, IDMDB, FIMAGIC, FIVEHMAGIC, SITE, VEHICLE, DESC001, DESC002, LOCN, PROGRESS, CHASSIS, REGN, CUSDATE, INVDATE, STYPE, TARMAGIC, VEHMAGIC, COST, REGDATE, DELDATE, EXEC001, INVOICE, INVTOT, STATUS, "LSV")YPE

The first thing I noticed is that when using multiLine, the column LSV_TYPE disappears. Instead it shows : string (nullable = true). When using df_ko.columns it shows something really odd: ..., "LSV")YPE. This does not happen when I use only \n as EOL. Setting quote option does not change anything.

The next thing I tested was the selection of the column FILIALE:

df_ok.select($"FILIALE")
res1: org.apache.spark.sql.DataFrame = [FILIALE: string]

df_ok.columns.contains("FILIALE")
res2: Boolean = true

df_ko.select($"FILIALE")
org.apache.spark.sql.AnalysisException: cannot resolve '`FILIALE`' given input columns: [INVOICE, DESC001, COST, STYPE, PROGRESS, INVTOT, VEHICLE, REGDATE, TARMAGIC, STATUS, CUSDATE, LOCN, INVDATE, SITE, DELDATE, REGN, EXEC001, VEHMAGIC,, DESC002, FIVEHMAGIC, CHASSIS, FIMAGIC, FILIALE];;

So I thought there is something more than in the name of he column:

df_ko.columns.head.toArray
res68: Array[Char] = Array(, F, I, L, I, A, L, E)

df_ko.columns.head.toArray.foreach(c => println(c.toInt))
65279
70
73
76
73
65
76
69

Than I checked the file differently:

spark.read.text("/.../20180423_LSV.csv").collect.head.getString(0).toArray.foreach(c => println(c.toInt))
70
73
76
73
65
76

An then I was sure the problem comes from the CSV parsing. Adding charset option or using univocity as the value for parseLib option does not change anything.

The file I'm using:

FILIALE|IDMDB|FIMAGIC|FIVEHMAGIC|SITE|VEHICLE|DESC001|DESC002|LOCN|PROGRESS|CHASSIS|REGN|CUSDATE|INVDATE|STYPE|TARMAGIC|VEHMAGIC|COST|REGDATE|DELDATE|EXEC001|INVOICE|INVTOT|STATUS|LSV_TYPE
XXXXXXXX|XX696209|XX696209|XX0|73|100284|XXXXXXXXXXXXX45XXXXXXXX|X|73X|X|XXX4503321X2361427|73XX100284||24X10X2005|X|696209|0|9592X7||22X10X2005|XXXX73|500228|10841X24|X|XX
XXXXXXXX|XX1454353|XX1454353|XX959136|73|100212|XXXXXXXXXXXXX45XXXXXXXXXXXXXXXXXX|XXXXXXXXXXXXX45XXXXXXXXXXXXXXXXXX|73X|X|XXX4503321X2096859|73XX100212||08X09X2005|X|1454353|959136|0|||XXXX73|500205|0|X|XX
XXXXXXXX|XX607020|XX607020|XX0|73|100097|XXXXXXXXXXXX50XXXXXXXXXXXXXX|X|73X|X|XXX4540001X0540628|8232XX33||17X02X2005|X|607020|0|10868X34|||XXXX73|500025|11750|X|XX
XXXXXXXX|XX1796002|XX1796002|XX0|73|100091|XXXXXXXXXXXX70XXXXXXXXXXX|X|73X|X|XXX4540011X072541X|73XX100091||21X01X2005|X|1796002|0|12457X44||19X01X2005|XXXX73|500010|13616X9|X|XX
XXXXXXXX|XX728637|XX728637|XX0|73|100046|XXXXXXXXXXXXX55XXXXXXXXXX|X|73X|X|XXX4503331X1326935|4059XX33||25X11X2005|X|728637|0|14425X76|22X02X2005|24X11X2005|XXXX73|500244|17500|X|XX
XXXXXXXX|XX555718|XX555718|XX0|73|100020|XXXXXXXXXXXXXXX45XXX|X|73X|X|XXX4524321X0392633|73XX100020||01X08X2005|X|555718|0|12897||29X07X2005|XXXX73|500173|17446X39|X|XX
XXXXXXXX|XX589182|XX589182|XX0|73|100270|XXXXXXXX1X1XXXXXXXX|X|73X|X|XXX4540301X0461656|73XX100270||19X09X2005|X|589182|0|13112X6||16X09X2005|XXXX73|500201|14998|X|XX
XXXXXXXX|XX1796399|XX1796399|XX0|73|100362|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|73X|X|XXX4503321X2775489|73XX100362||22X05X2006|X|1796399|0|10783X11||17X05X2006|XXXX73|500087|11976X24|X|XX
XXXXXXXX|XX1796399|XX1796399|XX0|73|100337|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|XXXXXXXXXXXXX45XXXXXXXXXXXXXX|73X|X|XXX4503321X2654339|73XX100337||22X05X2006|X|1796399|0|10672X11||17X05X2006|XXXX73|500086|11976X24|X|XX
XXXXXXXX|XX340211|XX340211|XX0|73|100383|XXXXXXXX50XXXXXXXXXXX|X|73X|X|XXX4540001X0839774|2724XX33||05X06X2006|X|340211|0|13321|23X05X2006|02X06X2006|XXXX73|500099|10999X99|X|XX

I executed my code on HDP 2.6.4 with Spark 2.2.0.

Does anyone has a workaround or an idea of what's happening ?

1

There are 1 best solutions below

0
On

Consider adding .option("ignoreTrailingWhiteSpace", true).

I was having the same issue and it resolved for me.