-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-15143][SPARK-15144][SQL] Add CSV tests with HadoopFsRelationTest and support for nullValue for other types #12921
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
Changes from all commits
ef71599
f80df8c
2ea702c
33288c8
f31f69e
1233bd7
75f1cb8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -431,7 +431,6 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils { | |
| } | ||
|
|
||
| test("nullable fields with user defined null value of \"null\"") { | ||
|
|
||
| // year,make,model,comment,blank | ||
| val dataSchema = StructType(List( | ||
| StructField("year", IntegerType, nullable = true), | ||
|
|
@@ -447,7 +446,7 @@ class CSVSuite extends QueryTest with SharedSQLContext with SQLTestUtils { | |
|
|
||
| verifyCars(cars, withHeader = true, checkValues = false) | ||
| val results = cars.collect() | ||
| assert(results(0).toSeq === Array(2012, "Tesla", "S", "null", "null")) | ||
| assert(results(0).toSeq === Array(2012, "Tesla", "S", null, null)) | ||
|
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. This is being tested against the data as below: Since the header is |
||
| assert(results(2).toSeq === Array(null, "Chevy", "Volt", null, null)) | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,10 +73,10 @@ class CSVTypeCastSuite extends SparkFunSuite { | |
|
|
||
| test("String type should always return the same as the input") { | ||
| assert( | ||
| CSVTypeCast.castTo("", StringType, nullable = true, CSVOptions()) == | ||
| CSVTypeCast.castTo("", StringType, nullable = true, CSVOptions("nullValue", null)) == | ||
| UTF8String.fromString("")) | ||
| assert( | ||
| CSVTypeCast.castTo("", StringType, nullable = false, CSVOptions()) == | ||
| CSVTypeCast.castTo("", StringType, nullable = false, CSVOptions("nullValue", null)) == | ||
|
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. @falaki I just noticed and thought this test implies
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. Otherwise, |
||
| UTF8String.fromString("")) | ||
| } | ||
|
|
||
|
|
@@ -180,5 +180,13 @@ class CSVTypeCastSuite extends SparkFunSuite { | |
| CSVTypeCast.castTo("-", DoubleType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DecimalType.DoubleDecimal, true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", StringType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", TimestampType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", DateType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| assertNull( | ||
| CSVTypeCast.castTo("-", BooleanType, nullable = true, CSVOptions("nullValue", "-"))) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 org.apache.spark.sql.sources | ||
|
|
||
| import org.apache.hadoop.fs.Path | ||
|
|
||
| import org.apache.spark.deploy.SparkHadoopUtil | ||
| import org.apache.spark.sql.types._ | ||
|
|
||
| class CSVHadoopFsRelationSuite extends HadoopFsRelationTest { | ||
| override val dataSourceName: String = "csv" | ||
|
|
||
| override val extraReadOptions: Map[String, String] = | ||
| Map("header" -> "true", "inferSchema" -> "true") | ||
|
|
||
| override val extraWriteOptions: Map[String, String] = Map("header" -> "true") | ||
|
|
||
| override protected def supportsDataType(dataType: DataType): Boolean = dataType match { | ||
| case _: NullType => false | ||
| // `StringType` test is too flaky. Seems random generate data affects delimiter | ||
| // for writing and CSV parse does not recognize this. | ||
| case _: StringType => false | ||
| case _: BinaryType => false | ||
| case _: CalendarIntervalType => false | ||
| case _: ArrayType => false | ||
| case _: MapType => false | ||
| case _: StructType => false | ||
| // Currently, this writes `DateType` and `TimestampType` as a long value. | ||
| // Since `dateFormat` is not yet supported for writing, this is disabled for now. | ||
| case _: DateType => false | ||
| case _: TimestampType => false | ||
| case _: UserDefinedType[_] => false | ||
| case _ => true | ||
| } | ||
|
|
||
| test("save()/load() - partitioned table - simple queries - partition columns in data") { | ||
| withTempDir { file => | ||
| val basePath = new Path(file.getCanonicalPath) | ||
| val fs = basePath.getFileSystem(SparkHadoopUtil.get.conf) | ||
| val qualifiedBasePath = fs.makeQualified(basePath) | ||
|
|
||
| for (p1 <- 1 to 2; p2 <- Seq("foo", "bar")) { | ||
| val partitionDir = new Path(qualifiedBasePath, s"p1=$p1/p2=$p2") | ||
| val header = Seq("a,b") | ||
| val data = (1 to 3).map(i => s"""$i,val_$i""") | ||
| sparkContext | ||
| .parallelize(header ++ data) | ||
| .saveAsTextFile(partitionDir.toString) | ||
| } | ||
|
|
||
| val dataSchemaWithPartition = | ||
| StructType(dataSchema.fields :+ StructField("p1", IntegerType, nullable = true)) | ||
|
|
||
| checkQueries( | ||
| hiveContext.read.format(dataSourceName) | ||
| .option("dataSchema", dataSchemaWithPartition.json) | ||
| .option("inferSchema", "true") | ||
| .option("header", "true") | ||
| .load(file.getCanonicalPath)) | ||
| } | ||
| } | ||
| } |
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.
Simply the logic below was added just like
inferField():