Aliases¶
Three kinds of alias rename three different things:
| Alias | Renames | Set with |
|---|---|---|
| Field alias | the Spark column | ts.field(df_alias=...) |
| Constructor alias | the constructor keyword | ts.field(alias=...) |
| DataFrame alias | the DataFrame, for joins | .alias(...) |
Field aliases¶
Map a Python attribute to a differently named Spark column with field(df_alias=...):
import typespark as ts
class Account(ts.DataFrame):
account_id: ts.String = ts.field(df_alias="ACCT_NO")
name: ts.String
from_df() reads ACCT_NO from the Spark DataFrame, generate_schema() names the
column ACCT_NO, and in Python it is account_id.
Constructor aliases¶
ts.field(alias=...) is attrs' own alias: it renames the keyword the constructor takes,
and nothing else. The attribute and the Spark column keep the field's name:
import typespark as ts
class Customer(ts.DataFrame):
name: ts.String
class Contact(ts.DataFrame):
full_name: ts.String = ts.field(alias="name")
def contacts(customers: Customer) -> Contact:
# The keyword is name
contact = Contact(customers, name=customers.name)
return contact # contact.full_name, Spark column full_name
Type checkers follow it: Contact(customers, full_name=...) is an error, the keyword is
name. Use it when the attribute name is awkward as a keyword. To change the Spark column
name, use df_alias= instead.
Known issue
With alias=, the field's docstring doesn't reach the
generated schema as a column comment.
DataFrame aliases¶
.alias() tags the whole DataFrame, and every typed column access carries its DataFrame's
alias automatically. See Joins.