-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathexpr.py
More file actions
36 lines (30 loc) · 1000 Bytes
/
expr.py
File metadata and controls
36 lines (30 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import xorq as xo
import xorq.expr.datatypes as dt
from xorq.caching import SourceStorage
@xo.udf.make_pandas_udf(
schema=xo.schema({"L_EXTENDEDPRICE": float, "L_DISCOUNT": float}),
return_type=dt.float,
name="calculate_discount_value"
)
def calculate_discount_value(df):
return df["L_EXTENDEDPRICE"] * df["L_DISCOUNT"]
snow = xo.snowflake.connect_env(schema="TPCH_SF1")
duckdb_con = xo.duckdb.connect()
storage = SourceStorage(duckdb_con)
expr = (
snow.table("LINEITEM")
.select(
xo._.L_QUANTITY.cast(float).name("L_QUANTITY"),
xo._.L_EXTENDEDPRICE.cast(float).name("L_EXTENDEDPRICE"),
xo._.L_DISCOUNT.cast(float).name("L_DISCOUNT"),
xo._.L_ORDERKEY
)
.into_backend(xo.connect())
.mutate(discount_value=calculate_discount_value.on_expr)
.group_by(xo._.L_ORDERKEY)
.agg(
xo._.discount_value.sum().name("total_discount"),
xo._.L_QUANTITY.sum().name("total_quantity")
)
.cache(storage=storage)
)