Joins¶
API Documentation
typespark.DataFrame.join
typespark.DataFrame.leftsemi
typespark.DataFrame.leftanti
typespark.JoinType
A join takes two typed DataFrames and returns a plain ts.DataFrame: its columns come from
both sides, so no single model describes it. Build the target model from it, taking each
column from the side it belongs to:
Models
import typespark as ts
class Order(ts.DataFrame):
order_id: ts.String
customer_id: ts.String
amount: ts.Double
class Customer(ts.DataFrame):
customer_id: ts.String
name: ts.String
country: ts.String
class OrderDetail(ts.DataFrame):
order_id: ts.String
customer_name: ts.String
country: ts.String
amount: ts.Double
def order_details(
orders: Order, customers: Customer
) -> OrderDetail:
joined = orders.join(
customers,
orders.customer_id == customers.customer_id,
"left",
)
return OrderDetail(
joined,
order_id=orders.order_id,
customer_name=customers.name,
country=customers.country,
amount=orders.amount,
)
def order_details_raw(
orders: DataFrame, customers: DataFrame
) -> DataFrame:
o = orders.alias("o")
c = customers.alias("c")
joined = o.join(
c,
F.col("o.customer_id") == F.col("c.customer_id"),
"left",
)
return joined.select(
F.col("o.order_id").alias("order_id"),
F.col("c.name").alias("customer_name"),
F.col("c.country").alias("country"),
F.col("o.amount").alias("amount"),
)
The columns of orders and customers stay valid on the joined DataFrame. Each column is
bound to the DataFrame it came from, so orders.customer_id and customers.customer_id
are different columns even though both are named customer_id: no alias("o") and no
"o.customer_id" strings. Every keyword is checked against OrderDetail, and the function
signature says exactly what goes in and comes out.
Self-joins¶
When both sides are the same DataFrame, the columns can't tell the sides apart on their
own. Give each side an alias with .alias(): every column taken from it carries the
alias.
Models
class Referral(ts.DataFrame):
customer_id: ts.String
referred_by: ts.String
class ReferralName(ts.DataFrame):
customer: ts.String
referrer: ts.String
def referral_names(
referrals: Referral, customers: Customer
) -> ReferralName:
customer = customers.alias("customer")
referrer = customers.alias("referrer")
joined = referrals.join(
customer, referrals.customer_id == customer.customer_id
).join(
referrer, referrals.referred_by == referrer.customer_id
)
return ReferralName(
joined,
customer=customer.name,
referrer=referrer.name,
)
def referral_names_raw(
referrals: DataFrame, customers: DataFrame
) -> DataFrame:
r = referrals.alias("r")
customer = customers.alias("customer")
referrer = customers.alias("referrer")
joined = r.join(
customer,
F.col("r.customer_id") == F.col("customer.customer_id"),
).join(
referrer,
F.col("r.referred_by") == F.col("referrer.customer_id"),
)
return joined.select(
F.col("customer.name").alias("customer"),
F.col("referrer.name").alias("referrer"),
)
Join types¶
join's how takes a ts.JoinType: a Literal of every join type Spark accepts, in all
its spellings. A misspelled join type is a type error instead of a failure when Spark
runs:
on = orders.customer_id == customers.customer_id
# also "leftouter", "left_outer"
orders.join(customers, on, "left")
orders.join(customers, on, "full_outer")
orders.join(customers, on, "left_outr") # type error
| Join | Accepted spellings |
|---|---|
| inner | "inner" |
| cross | "cross" |
| full outer | "outer", "full", "fullouter", "full_outer" |
| left outer | "left", "leftouter", "left_outer" |
| right outer | "right", "rightouter", "right_outer" |
| left semi | "semi", "leftsemi", "left_semi" |
| left anti | "anti", "leftanti", "left_anti" |
join returns a plain ts.DataFrame, since its columns come from both sides: build the
target model from it. Semi and anti joins only filter, so they have their own methods that
keep the model:
known = orders.leftsemi(customers, on) # an Order
unknown = orders.leftanti(customers, on) # an Order
Use ts.JoinType in your own signatures to pass a join type through:
def combine(
orders: Order, customers: Customer, how: ts.JoinType
) -> ts.DataFrame:
on = orders.customer_id == customers.customer_id
return orders.join(customers, on, how)