Error messages¶
TypeSpark checks what it can before Spark runs, and fails with errors that say what to
fix. All of them are exported from typespark and subclass a built-in exception, so they
can be caught specifically or generally:
try:
person = Person.from_df(raw_df)
except ts.MissingColumnError as e:
print(e.field_name, e.available_columns)
| Error | Subclass of | Raised when |
|---|---|---|
MissingColumnError |
ValueError |
the data lacks a column the model needs |
InvalidDefaultColumnError |
TypeError |
a factory= doesn't return a typed column |
InvalidLiteralDefaultError |
ValueError |
a literal default= can't apply to the field |
ExplodeAccessError |
ValueError |
an exploded element is used outside explode(access) |
LazyDataFrameError |
RuntimeError |
a lazy DataFrame is asked for data |
UnsupportedSparkFeatureError |
RuntimeError |
a function needs a newer Spark |
MissingColumnError¶
The data has no column for a field that has no default, when wrapping with from_df()
or creating with create().
Missing required column 'age' for field 'age' in Person. Available columns: ['name'].
To fix: provide the column, adjust aliases, or set a default for 'age'.
For a field inside a struct, the message names the struct column:
Missing required column 'zip' for field 'zip' in Address (nested under struct column
'address'). Available columns: ['city']. To fix: provide the subfield, adjust aliases,
or set a default on the struct field 'address'.
Fix: check the column name in the data against the model. If they differ on purpose,
map them with df_alias=; if the column is
optional, give the field a default. Struct subfields aren't
filled from their own defaults on read: put the default on the struct field (see
Struct subfield defaults).
The error has field_name and available_columns attributes for handling it in code.
InvalidDefaultColumnError¶
A factory= returned something other than a typed column.
Default for T.name must be a Column. Got str: 'Doe'. Field alias: 'name'.
Use factory=lambda: <Column>(...) for a lazy default.
Fix: a plain value goes in default= (default="Doe"); factory= is for column
expressions, e.g. factory=lambda: ts.Date(F.current_date()). See
Defaults.
InvalidLiteralDefaultError¶
A literal default= can't apply to the field. Two cases:
Invalid default for T.name: None. The field is declared nullable=False.
Invalid default for T.p: 'x'. P is a struct or array; only None is supported as a
literal default. Use factory= for anything else.
Fix: a non-nullable field needs a non-None default, or none at all. A struct or array
field takes None or a factory=, e.g. factory=lambda: Props() for a struct built from
its own defaults.
ExplodeAccessError¶
An exploded element was used where the explode doesn't run. Three cases:
Reading a field off the bare element:
String<'__ts_explode_0['id'] AS area_id'> uses an exploded element outside explode().
Put the whole expression in the access function, e.g.
`df.items.explode(lambda x: x.id)` instead of `df.items.explode().id`.
Exploding in withColumn, which doesn't build model columns:
withColumn('x', ...) got a column built with arr.explode(...), which builds model
columns: use it in the constructor. To add an exploded column outside the model, use
tsf.explode(arr).
Exploding two arrays in one constructor:
Only one array can be exploded per projection; got 2. Explode them in separate steps.
Fix: follow the message. See Generators for how exploding works and its rules.
LazyDataFrameError¶
A lazy DataFrame (Model.lazy()) has columns but no
data, and something asked it for data.
Person.lazy('p') holds column references only — it has no underlying DataFrame.
Data operations (to_df, to_spark, filter, select, show, write, ...) are unavailable.
Fix: use lazy() only to build column expressions, such as join or merge conditions.
For data, wrap a real DataFrame with from_df().
UnsupportedSparkFeatureError¶
A typed function uses a feature the installed PySpark lacks. TypeSpark doesn't emulate it.
tsf.array_position requires Spark >= 4.0 (pyspark 3.5.6 is installed).
Fix: upgrade PySpark, or avoid the function on older versions. The affected functions
are listed under Spark 4 functions. The error has
feature and min_spark attributes.
API reference¶
MissingColumnError ¶
InvalidDefaultColumnError ¶
InvalidLiteralDefaultError ¶
Bases: ValueError
ExplodeAccessError ¶
Bases: ValueError
An exploded element was used outside explode(access), or two arrays
were exploded in one projection.
LazyDataFrameError ¶
Bases: RuntimeError