为什么即使指定了所有值,Spark SQL 也会为字符串列打开可为空?

Why does Spark SQL turn nullable on for string column even when all values specified?

所以对于这样的事情:

case class RandomClass(stringOne: String, stringTwo: String, numericOne: Int)
val ds = Seq(
  RandomClass("a", null, 1),  
  RandomClass("a", "x", 3), 
  RandomClass("a", "y", 4), 
  RandomClass("a", null, 5)
).toDS()

ds.printSchema()

结果

root
 |-- stringOne: string (nullable = true)
 |-- stringTwo: string (nullable = true)
 |-- numericOne: integer (nullable = false)

为什么 stringOne 会是 nullable? 奇怪的是, numericOne 是正确推断的。我想我只是遗漏了一些关于 Dataset 和 DataFrame 之间关系的信息 API?

why would stringOne be nullable

因为 Scala String 只是一个 Java 字符串,不像 Scala Int 可以是 null。实际内容(存在或不存在 null 值)根本无关紧要。

另见

的确,Spark 会根据推断的类型是位于 Scala 对象层次结构的 AnyRef 还是 AnyVal 一侧,对可空性做出最佳猜测,但也请注意,它可以是比那更复杂。例如,当使用 Parquet 文件时,出于兼容性目的,everything 被推断为 nullable

同时,当您创建架构时,如果您愿意,可以在任何地方简单地设置 nullable = true

StructField(fieldName, LongType, nullable = true)

// or using a "DSL"
$"fieldName".long.copy(nullable = false)