Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add q4 implementation #891

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tpch/execute/q4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from pathlib import Path

import pandas as pd
import polars as pl
from queries import q4

pd.options.mode.copy_on_write = True
pd.options.future.infer_string = True

line_item = Path("data") / "lineitem.parquet"
orders = Path("data") / "orders.parquet"

IO_FUNCS = {
"pandas": lambda x: pd.read_parquet(x, engine="pyarrow"),
"pandas[pyarrow]": lambda x: pd.read_parquet(
x, engine="pyarrow", dtype_backend="pyarrow"
),
"polars[eager]": lambda x: pl.read_parquet(x),
"polars[lazy]": lambda x: pl.scan_parquet(x),
}

tool = "pandas"
fn = IO_FUNCS[tool]
print(q4.query(fn(line_item), fn(orders)))

tool = "pandas[pyarrow]"
fn = IO_FUNCS[tool]
print(q4.query(fn(line_item), fn(orders)))

tool = "polars[eager]"
fn = IO_FUNCS[tool]
print(q4.query(fn(line_item), fn(orders)))

tool = "polars[lazy]"
fn = IO_FUNCS[tool]
print(q4.query(fn(line_item), fn(orders)).collect())
31 changes: 31 additions & 0 deletions tpch/queries/q4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import datetime

import narwhals as nw
from narwhals.typing import FrameT


@nw.narwhalify
def query(
line_item_ds_raw: FrameT,
orders_ds_raw: FrameT,
) -> FrameT:
var_1 = datetime(1993, 7, 1)
var_2 = datetime(1993, 10, 1)

line_item_ds = nw.from_native(line_item_ds_raw)
orders_ds = nw.from_native(orders_ds_raw)

result = (
line_item_ds.join(orders_ds, left_on="l_orderkey", right_on="o_orderkey")
.filter(
nw.col("o_orderdate").is_between(var_1, var_2, closed="left"),
nw.col("l_commitdate") < nw.col("l_receiptdate"),
)
.unique(subset=["o_orderpriority", "l_orderkey"])
.group_by("o_orderpriority")
.agg(nw.len().alias("order_count"))
.sort(by="o_orderpriority")
.with_columns(nw.col("order_count").cast(nw.Int64))
)

return nw.to_native(result)
Loading