forked from databricks/spark-csv
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCsvSuite.scala
More file actions
executable file
·739 lines (610 loc) · 24.4 KB
/
Copy pathCsvSuite.scala
File metadata and controls
executable file
·739 lines (610 loc) · 24.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/*
* Copyright 2014 Databricks
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.databricks.spark.csv
import java.io.File
import java.nio.charset.UnsupportedCharsetException
import java.sql.Timestamp
import com.databricks.spark.csv.util.ParseModes
import org.apache.hadoop.io.compress.GzipCodec
import org.apache.spark.sql.{SQLContext, Row, SaveMode}
import org.apache.spark.{SparkContext, SparkException}
import org.apache.spark.sql.types._
import org.scalatest.{BeforeAndAfterAll, FunSuite}
abstract class AbstractCsvSuite extends FunSuite with BeforeAndAfterAll {
val carsFile = "src/test/resources/cars.csv"
val carsFile8859 = "src/test/resources/cars_iso-8859-1.csv"
val carsTsvFile = "src/test/resources/cars.tsv"
val carsAltFile = "src/test/resources/cars-alternative.csv"
val carsUnbalancedQuotesFile = "src/test/resources/cars-unbalanced-quotes.csv"
val nullNumbersFile = "src/test/resources/null-numbers.csv"
val nullNullNumbersFile = "src/test/resources/null_null_numbers.csv"
val nullSlashNNumbersFile = "src/test/resources/null_slashn_numbers.csv"
val emptyFile = "src/test/resources/empty.csv"
val ageFile = "src/test/resources/ages.csv"
val escapeFile = "src/test/resources/escape.csv"
val tempEmptyDir = "target/test/empty/"
val commentsFile = "src/test/resources/comments.csv"
val disableCommentsFile = "src/test/resources/disable_comments.csv"
private val simpleDatasetFile = "src/test/resources/simple.csv"
val numCars = 3
protected def parserLib: String
private var sqlContext: SQLContext = _
override protected def beforeAll(): Unit = {
super.beforeAll()
sqlContext = new SQLContext(new SparkContext("local[2]", "CsvSuite"))
}
override protected def afterAll(): Unit = {
try {
sqlContext.sparkContext.stop()
} finally {
super.afterAll()
}
}
test("DSL test") {
val results = sqlContext
.csvFile(carsFile, parserLib = parserLib)
.select("year")
.collect()
assert(results.size === numCars)
}
test("DSL test for iso-8859-1 encoded file") {
// scalastyle:off
val dataFrame = sqlContext
.csvFile(carsFile8859, parserLib = parserLib, charset = "iso-8859-1", delimiter = 'þ')
assert(dataFrame.select("year").collect().size === numCars)
val results = dataFrame.select("comment", "year").where(dataFrame("year") === "1997")
assert(results.first.getString(0) === "Go get one now they are þoing fast")
// scalastyle:on
}
test("DSL test bad charset name") {
val exception = intercept[UnsupportedCharsetException] {
val results = sqlContext
.csvFile(carsFile8859, parserLib = parserLib, charset = "1-9588-osi")
.select("year")
.collect()
}
assert(exception.getMessage.contains("1-9588-osi"))
}
test("DDL test") {
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTable
|USING com.databricks.spark.csv
|OPTIONS (path "$carsFile", header "true", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT year FROM carsTable").collect().size === numCars)
}
test("DDL test with alias name") {
assume(org.apache.spark.SPARK_VERSION.take(3) >= "1.5",
"Datasource alias feature was added in Spark 1.5")
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTsvTable
|USING csv
|OPTIONS (path "$carsTsvFile", header "true", delimiter "\t", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT year FROM carsTable").collect().size === numCars)
}
test("DDL test with charset") {
// scalastyle:off
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTable
|USING com.databricks.spark.csv
|OPTIONS (path "$carsFile8859", header "true", parserLib "$parserLib",
|charset "iso-8859-1", delimiter "þ")
""".stripMargin.replaceAll("\n", " "))
//scalstyle:on
assert(sqlContext.sql("SELECT year FROM carsTable").collect().size === numCars)
}
test("DDL test with tab separated file") {
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTable
|USING com.databricks.spark.csv
|OPTIONS (path "$carsTsvFile", header "true", delimiter "\t", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT year FROM carsTable").collect().size === numCars)
}
test("DDL test parsing decimal type") {
assume(org.apache.spark.SPARK_VERSION.take(3) > "1.3",
"DecimalType is broken on Spark 1.3.x")
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTable
|(yearMade double, makeName string, modelName string, priceTag decimal,
| comments string, grp string)
|USING com.databricks.spark.csv
|OPTIONS (path "$carsTsvFile", header "true", delimiter "\t", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT yearMade FROM carsTable").collect().size === numCars)
assert(
sqlContext.sql("SELECT makeName FROM carsTable where priceTag > 60000").collect().size === 1)
}
test("DSL test for DROPMALFORMED parsing mode") {
val results = new CsvParser()
.withParseMode(ParseModes.DROP_MALFORMED_MODE)
.withUseHeader(true)
.withParserLib(parserLib)
.csvFile(sqlContext, carsFile)
.select("year")
.collect()
assert(results.size === numCars - 1)
}
test("DSL test for DROPMALFORMED parsing mode with pruned scan") {
val strictSchema = new StructType(
Array(
StructField("Name", StringType, true),
StructField("Age", IntegerType, true),
StructField("Height", DoubleType, true),
StructField("Born", TimestampType, true)
)
)
val results = new CsvParser()
.withSchema(strictSchema)
.withUseHeader(true)
.withParserLib(parserLib)
.withParseMode(ParseModes.DROP_MALFORMED_MODE)
.csvFile(sqlContext, ageFile)
.select("Name")
.collect().size
assert(results === 1)
}
test("DSL test for FAILFAST parsing mode") {
val parser = new CsvParser()
.withParseMode(ParseModes.FAIL_FAST_MODE)
.withUseHeader(true)
.withParserLib(parserLib)
val exception = intercept[SparkException]{
parser.csvFile(sqlContext, carsFile)
.select("year")
.collect()
}
assert(exception.getMessage.contains("Malformed line in FAILFAST mode: 2015,Chevy,Volt"))
}
test("DSL test roundtrip nulls") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "null-numbers.csv"
val agesSchema = StructType(List(StructField("name", StringType, true),
StructField("age", IntegerType, true)))
val agesRows = Seq(Row("alice", 35), Row("bob", null), Row(null, 24))
val agesRdd = sqlContext.sparkContext.parallelize(agesRows)
val agesDf = sqlContext.createDataFrame(agesRdd, agesSchema)
agesDf.saveAsCsvFile(copyFilePath, Map("header" -> "true", "nullValue" -> ""))
val agesCopy = new CsvParser()
.withSchema(agesSchema)
.withUseHeader(true)
.withTreatEmptyValuesAsNulls(true)
.withParserLib(parserLib)
.csvFile(sqlContext, copyFilePath)
assert(agesCopy.count == agesRows.size)
assert(agesCopy.collect.toSet == agesRows.toSet)
}
test("DSL test with alternative delimiter and quote") {
val results = new CsvParser()
.withDelimiter('|')
.withQuoteChar('\'')
.withUseHeader(true)
.withParserLib(parserLib)
.csvFile(sqlContext, carsAltFile)
.select("year")
.collect()
assert(results.size === numCars)
}
test("DSL test with null quote character") {
val results = new CsvParser()
.withDelimiter(',')
.withQuoteChar(null)
.withUseHeader(true)
.withParserLib(parserLib)
.csvFile(sqlContext, carsUnbalancedQuotesFile)
.select("year")
.collect()
assert(results.size === numCars)
}
test("DSL test with alternative delimiter and quote using sparkContext.csvFile") {
val results =
sqlContext.csvFile(
carsAltFile,
useHeader = true,
delimiter = '|',
quote = '\'',
parserLib = parserLib)
.select("year")
.collect()
assert(results.size === numCars)
}
test("Expect parsing error with wrong delimiter setting using sparkContext.csvFile") {
intercept[ org.apache.spark.sql.AnalysisException] {
sqlContext.csvFile(
carsAltFile,
useHeader = true,
delimiter = ',',
quote = '\'',
parserLib = parserLib)
.select("year")
.collect()
}
}
test("Expect wrong parsing results with wrong quote setting using sparkContext.csvFile") {
val results =
sqlContext.csvFile(
carsAltFile,
useHeader = true,
delimiter = '|',
quote = '"',
parserLib = parserLib)
.select("year")
.collect()
assert(results.slice(0, numCars).toSeq.map(_(0).asInstanceOf[String]) ==
Seq("'2012'", "1997", "2015"))
}
test("DDL test with alternative delimiter and quote") {
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTable
|USING com.databricks.spark.csv
|OPTIONS (path "$carsAltFile", header "true", quote "'", delimiter "|",
|parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT year FROM carsTable").collect().size === numCars)
}
test("DSL test with empty file and known schema") {
val results = new CsvParser()
.withSchema(StructType(List(StructField("column", StringType, false))))
.withUseHeader(false)
.withParserLib(parserLib)
.csvFile(sqlContext, emptyFile)
.count()
assert(results === 0)
}
test("DSL test with poorly formatted file and string schema") {
val stringSchema = new StructType(
Array(
StructField("Name", StringType, true),
StructField("Age", StringType, true),
StructField("Height", StringType, true),
StructField("Born", StringType, true)
)
)
val results = new CsvParser()
.withSchema(stringSchema)
.withUseHeader(true)
.withParserLib(parserLib)
.withParseMode(ParseModes.DROP_MALFORMED_MODE)
.csvFile(sqlContext, ageFile)
.count()
assert(results === 4)
}
test("DSL test with poorly formatted file and known schema") {
val strictSchema = new StructType(
Array(
StructField("Name", StringType, true),
StructField("Age", IntegerType, true),
StructField("Height", DoubleType, true),
StructField("Born", TimestampType, true)
)
)
val results = new CsvParser()
.withSchema(strictSchema)
.withUseHeader(true)
.withParserLib(parserLib)
.withParseMode(ParseModes.DROP_MALFORMED_MODE)
.csvFile(sqlContext, ageFile)
.count()
assert(results === 1)
}
test("DDL test with empty file") {
sqlContext.sql(s"""
|CREATE TEMPORARY TABLE carsTable
|(yearMade double, makeName string, modelName string, comments string, grp string)
|USING com.databricks.spark.csv
|OPTIONS (path "$emptyFile", header "false", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT count(*) FROM carsTable").collect().head(0) === 0)
}
test("DDL test with schema") {
sqlContext.sql(s"""
|CREATE TEMPORARY TABLE carsTable
|(yearMade double, makeName string, modelName string, comments string, grp string)
|USING com.databricks.spark.csv
|OPTIONS (path "$carsFile", header "true", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT makeName FROM carsTable").collect().size === numCars)
assert(sqlContext.sql("SELECT avg(yearMade) FROM carsTable where grp = '' group by grp")
.collect().head(0) === 2004.5)
}
test("DSL column names test") {
val cars = new CsvParser()
.withUseHeader(false)
.withParserLib(parserLib)
.csvFile(sqlContext, carsFile)
assert(cars.schema.fields(0).name == "C0")
assert(cars.schema.fields(2).name == "C2")
}
test("SQL test insert overwrite") {
// Create a temp directory for table that will be overwritten
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTableIO
|USING com.databricks.spark.csv
|OPTIONS (path "$carsFile", header "true", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
sqlContext.sql(s"""
|CREATE TEMPORARY TABLE carsTableEmpty
|(yearMade double, makeName string, modelName string, comments string, grp string)
|USING com.databricks.spark.csv
|OPTIONS (path "$tempEmptyDir", header "false", parserLib "$parserLib")
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT * FROM carsTableIO").collect().size === numCars)
assert(sqlContext.sql("SELECT * FROM carsTableEmpty").collect().isEmpty)
sqlContext.sql(
s"""
|INSERT OVERWRITE TABLE carsTableEmpty
|SELECT * FROM carsTableIO
""".stripMargin.replaceAll("\n", " "))
assert(sqlContext.sql("SELECT * FROM carsTableEmpty").collect().size == numCars)
}
test("DSL save") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "cars-copy.csv"
val cars = sqlContext.csvFile(carsFile, parserLib = parserLib)
cars.saveAsCsvFile(copyFilePath, Map("header" -> "true"))
val carsCopy = sqlContext.csvFile(copyFilePath + "/")
assert(carsCopy.count == cars.count)
assert(carsCopy.collect.map(_.toString).toSet == cars.collect.map(_.toString).toSet)
}
test("DSL save with a compression codec") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "cars-copy.csv"
val cars = sqlContext.csvFile(carsFile, parserLib = parserLib)
cars.saveAsCsvFile(copyFilePath, Map("header" -> "true"), classOf[GzipCodec])
val carsCopy = sqlContext.csvFile(copyFilePath + "/")
assert(carsCopy.count == cars.count)
assert(carsCopy.collect.map(_.toString).toSet == cars.collect.map(_.toString).toSet)
}
test("Scala API save with gzip compression codec") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "cars-copy.csv"
val cars = sqlContext.csvFile(carsFile, parserLib = parserLib)
cars.save("com.databricks.spark.csv", SaveMode.Overwrite,
Map("path" -> copyFilePath, "header" -> "true", "codec" -> classOf[GzipCodec].getName))
val carsCopyPartFile = new File(copyFilePath, "part-00000.gz")
// Check that the part file has a .gz extension
assert(carsCopyPartFile.exists())
val carsCopy = sqlContext.csvFile(copyFilePath + "/")
assert(carsCopy.count == cars.count)
assert(carsCopy.collect.map(_.toString).toSet == cars.collect.map(_.toString).toSet)
}
test("Scala API save with gzip compression codec by shorten name") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "cars-copy.csv"
val cars = sqlContext.csvFile(carsFile, parserLib = parserLib)
cars.save("com.databricks.spark.csv", SaveMode.Overwrite,
Map("path" -> copyFilePath, "header" -> "true", "codec" -> "gZiP"))
val carsCopyPartFile = new File(copyFilePath, "part-00000.gz")
// Check that the part file has a .gz extension
assert(carsCopyPartFile.exists())
val carsCopy = sqlContext.csvFile(copyFilePath + "/")
assert(carsCopy.count == cars.count)
assert(carsCopy.collect.map(_.toString).toSet == cars.collect.map(_.toString).toSet)
}
test("DSL save with quoting") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "cars-copy.csv"
val cars = sqlContext.csvFile(carsFile, parserLib = parserLib)
cars.saveAsCsvFile(copyFilePath, Map("header" -> "true", "quote" -> "\""))
val carsCopy = sqlContext.csvFile(copyFilePath + "/", parserLib = parserLib)
assert(carsCopy.count == cars.count)
assert(carsCopy.collect.map(_.toString).toSet == cars.collect.map(_.toString).toSet)
}
test("DSL save with alternate quoting") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "cars-copy.csv"
val cars = sqlContext.csvFile(carsFile)
cars.saveAsCsvFile(copyFilePath, Map("header" -> "true", "quote" -> "!"))
val carsCopy = sqlContext.csvFile(copyFilePath + "/", quote = '!', parserLib = parserLib)
assert(carsCopy.count == cars.count)
assert(carsCopy.collect.map(_.toString).toSet == cars.collect.map(_.toString).toSet)
}
test("DSL save with quoting, escaped quote") {
// Create temp directory
TestUtils.deleteRecursively(new File(tempEmptyDir))
new File(tempEmptyDir).mkdirs()
val copyFilePath = tempEmptyDir + "escape-copy.csv"
val escape = sqlContext.csvFile(escapeFile, escape = '|', quote = '"')
escape.saveAsCsvFile(copyFilePath, Map("header" -> "true", "quote" -> "\""))
val escapeCopy = sqlContext.csvFile(copyFilePath + "/", parserLib = parserLib)
assert(escapeCopy.count == escape.count)
assert(escapeCopy.collect.map(_.toString).toSet == escape.collect.map(_.toString).toSet)
assert(escapeCopy.head().getString(0) == "\"thing")
}
test("DSL test schema inferred correctly") {
val results = sqlContext
.csvFile(carsFile, parserLib = parserLib, inferSchema = true)
assert(results.schema == StructType(List(
StructField("year", IntegerType, nullable = true),
StructField("make", StringType, nullable = true),
StructField("model", StringType ,nullable = true),
StructField("comment", StringType, nullable = true),
StructField("blank", StringType, nullable = true))
))
assert(results.collect().size === numCars)
}
test("DSL test inferred schema passed through") {
val dataFrame = sqlContext
.csvFile(carsFile, parserLib = parserLib, inferSchema = true)
val results = dataFrame
.select("comment", "year")
.where(dataFrame("year") === 2012)
assert(results.first.getString(0) === "No comment")
assert(results.first.getInt(1) === 2012)
}
test("DDL test with inferred schema") {
sqlContext.sql(
s"""
|CREATE TEMPORARY TABLE carsTable
|USING com.databricks.spark.csv
|OPTIONS (path "$carsFile", header "true", parserLib "$parserLib", inferSchema "true")
""".stripMargin.replaceAll("\n", " "))
val results = sqlContext.sql("select year from carsTable where make = 'Ford'")
assert(results.first().getInt(0) === 1997)
}
test("DSL test nullable fields") {
val results = new CsvParser()
.withSchema(StructType(List(StructField("name", StringType, false),
StructField("age", IntegerType, true))))
.withUseHeader(true)
.withParserLib(parserLib)
.csvFile(sqlContext, nullNumbersFile)
.collect()
assert(results.head.toSeq === Seq("alice", 35))
assert(results(1).toSeq === Seq("bob", null))
assert(results(2).toSeq === Seq("", 24))
}
test("DSL test nullable fields with user defined null value of \"null\"") {
val results = new CsvParser()
.withSchema(StructType(List(StructField("name", StringType, false),
StructField("age", IntegerType, true))))
.withUseHeader(true)
.withParserLib(parserLib)
.withNullValue("null")
.csvFile(sqlContext, nullNullNumbersFile)
.collect()
assert(results.head.toSeq === Seq("alice", 35))
assert(results(1).toSeq === Seq("bob", null))
assert(results(2).toSeq === Seq("null", 24))
}
test("DSL test nullable fields with user defined null value of \"\\N\"") {
val results = new CsvParser()
.withSchema(StructType(List(StructField("name", StringType, false),
StructField("age", IntegerType, true))))
.withUseHeader(true)
.withParserLib(parserLib)
.withNullValue("\\N")
.csvFile(sqlContext, nullSlashNNumbersFile)
.collect()
assert(results.head.toSeq === Seq("alice", 35))
assert(results(1).toSeq === Seq("bob", null))
assert(results(2).toSeq === Seq("\\N", 24))
}
test("Commented lines in CSV data") {
val results: Array[Row] = new CsvParser()
.withDelimiter(',')
.withComment('~')
.withParserLib(parserLib)
.csvFile(sqlContext, commentsFile)
.collect()
val expected =
Seq(Seq("1", "2", "3", "4", "5.01", "2015-08-20 15:57:00"),
Seq("6", "7", "8", "9", "0", "2015-08-21 16:58:01"),
Seq("1", "2", "3", "4", "5", "2015-08-23 18:00:42"))
assert(results.toSeq.map(_.toSeq) === expected)
}
test("Inferring schema") {
val results: Array[Row] = new CsvParser()
.withDelimiter(',')
.withComment('~')
.withParserLib(parserLib)
.withInferSchema(true)
.csvFile(sqlContext, commentsFile)
.collect()
val expected =
Seq(Seq(1, 2, 3, 4, 5.01D, Timestamp.valueOf("2015-08-20 15:57:00")),
Seq(6, 7, 8, 9, 0, Timestamp.valueOf("2015-08-21 16:58:01")),
Seq(1, 2, 3, 4, 5, Timestamp.valueOf("2015-08-23 18:00:42")))
assert(results.toSeq.map(_.toSeq) === expected)
}
test("Setting comment to null disables comment support") {
val results: Array[Row] = new CsvParser()
.withDelimiter(',')
.withComment(null)
.withParserLib(parserLib)
.csvFile(sqlContext, disableCommentsFile)
.collect()
val expected =
Seq(
Seq("#1", "2", "3"),
Seq("4", "5", "6"))
assert(results.toSeq.map(_.toSeq) === expected)
}
test("DSL load csv from rdd") {
val csvRdd = sqlContext.sparkContext.parallelize(Seq("age,height", "20,1.8", "16,1.7"))
val df = new CsvParser()
.withUseHeader(true)
.withParserLib(parserLib)
.csvRdd(sqlContext, csvRdd)
.collect()
assert(df(0).toSeq === Seq("20", "1.8"))
assert(df(1).toSeq === Seq("16", "1.7"))
}
test("Inserting into csvRdd should throw exception"){
val csvRdd = sqlContext.sparkContext.parallelize(Seq("age,height", "20,1.8", "16,1.7"))
val sampleData = sqlContext.sparkContext.parallelize(Seq("age,height", "20,1.8", "16,1.7"))
val df = new CsvParser()
.withUseHeader(true)
.withParserLib(parserLib)
.csvRdd(sqlContext, csvRdd)
val sampleDf = new CsvParser()
.withUseHeader(true)
.withParserLib(parserLib)
.csvRdd(sqlContext, sampleData)
df.registerTempTable("csvRdd")
sampleDf.registerTempTable("sampleDf")
val exception = intercept[java.io.IOException] {
sqlContext.sql("INSERT OVERWRITE TABLE csvRdd select * from sampleDf")
}
assert(exception.getMessage.contains("Cannot INSERT into table with no path defined"))
}
test("DSL tsv test") {
val results = sqlContext
.tsvFile(carsTsvFile, parserLib = parserLib)
.select("year")
.collect()
assert(results.size === numCars)
}
test("Type/Schema inference works as expected for the simple sparse dataset.") {
val df = new CsvParser()
.withUseHeader(true)
.withInferSchema(true)
.csvFile(sqlContext, simpleDatasetFile)
assert(
df.schema.fields.map(_.dataType).deep ==
Array(IntegerType, IntegerType, IntegerType, IntegerType).deep)
}
}
class CsvSuite extends AbstractCsvSuite {
override def parserLib: String = "COMMONS"
}
class CsvFastSuite extends AbstractCsvSuite {
override def parserLib: String = "UNIVOCITY"
}