I am reading a delta table like this
df = (spark
.read
.format("delta")
.load("/path/to/foo/table")
.select("Foo_ID", "Bar")
)
now when I look at the DataFrame schema, spark has somehow incorrectly inferred nullability:
>>> df.schema
...
StructType([StructField('Foo_ID', LongType(), True), StructField('Bar', TimestampType(), True)])
I know the values should not be null and I want the schema to reflect that by imposing a not null constraint on the columns. In case I try to load a table that actually does contain a null I want to see a big fat error message, rather than silently switching. Is there a clean way to do this? I came up with the following workaround, but it doesn't feel very nice:
my_schema = StructType([StructField('Foo_ID', LongType(), False), StructField('Bar', TimestampType(), False)])
df2 = spark.createDataFrame(df.rdd, schema=my_schema)
Is there a better way?