Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion python/pyspark/sql/streaming.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,7 +839,7 @@ def _test():
globs['sqlContext'] = SQLContext.getOrCreate(spark.sparkContext)
globs['sdf'] = \
spark.readStream.format('text').load('python/test_support/sql/streaming')
globs['sdf_schema'] = StructType([StructField("data", StringType(), False)])
globs['sdf_schema'] = StructType([StructField("data", StringType(), True)])
globs['df'] = \
globs['spark'].readStream.format('text').load('python/test_support/sql/streaming')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class DataFrameReader private[sql](sparkSession: SparkSession) extends Logging {
* @since 1.4.0
*/
def schema(schema: StructType): DataFrameReader = {
this.userSpecifiedSchema = Option(schema)
this.userSpecifiedSchema = Option(schema.asNullable)
this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ final class DataStreamReader private[sql](sparkSession: SparkSession) extends Lo
* @since 2.0.0
*/
def schema(schema: StructType): DataStreamReader = {
this.userSpecifiedSchema = Option(schema)
this.userSpecifiedSchema = Option(schema.asNullable)
this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,18 @@ class FileStreamSourceSuite extends FileStreamSourceTest {
newSource.getBatch(None, FileStreamSourceOffset(1))
}
}

test("Force user specified schema to the nullable") {
withTempDir { src =>
stringToFile(new File(src, "1"), "a\nb\nc")
val userSchema =
StructType(StructField("a", StringType, nullable = false) :: Nil)
val schema = createFileStreamSourceAndGetSchema(
format = Some("parquet"), path = Some(src.getCanonicalPath), schema = Some(userSchema))
assert(schema != userSchema)
assert(schema === userSchema.asNullable)
}
}
}

class FileStreamSourceStressTestSuite extends FileStreamSourceTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -635,4 +635,12 @@ class DataFrameReaderWriterSuite extends QueryTest with SharedSQLContext with Be
checkAnswer(spark.table("t"), Row(1, "a") :: Row(2, "b") :: Nil)
}
}

test("SPARK-16472: Force user specified schema to the nullable") {
val schema = new StructType().add("s", StringType, nullable = false)
val nullableSchema = schema.asNullable
val df = spark.read.schema(schema).text()
assert(df.schema != schema)
assert(df.schema == nullableSchema)
}
}