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: support more scalar operations for duckdb, Increase width for ipython #1877

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
29 changes: 24 additions & 5 deletions narwhals/_duckdb/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,27 @@ def select(
*exprs: DuckDBExpr,
**named_exprs: DuckDBExpr,
) -> Self:
new_columns_map = parse_exprs_and_named_exprs(self)(*exprs, **named_exprs)
new_columns_map, returns_scalar = parse_exprs_and_named_exprs(self)(
*exprs, **named_exprs
)
if not new_columns_map:
# TODO(marco): return empty relation with 0 columns?
return self._from_native_frame(self._native_frame.limit(0))

if all(getattr(x, "_returns_scalar", False) for x in exprs) and all(
getattr(x, "_returns_scalar", False) for x in named_exprs.values()
):
if all(returns_scalar):
return self._from_native_frame(
self._native_frame.aggregate(
[val.alias(col) for col, val in new_columns_map.items()]
)
)
if any(returns_scalar):
msg = (
"Mixing expressions which aggregate and expressions which don't\n"
"is not yet supported by the DuckDB backend. Once they introduce\n"
"duckdb.WindowExpression to their Python API, we'll be able to\n"
"support this."
)
raise NotImplementedError(msg)

return self._from_native_frame(
self._native_frame.select(
Expand All @@ -139,7 +147,18 @@ def with_columns(
*exprs: DuckDBExpr,
**named_exprs: DuckDBExpr,
) -> Self:
new_columns_map = parse_exprs_and_named_exprs(self)(*exprs, **named_exprs)
new_columns_map, returns_scalar = parse_exprs_and_named_exprs(self)(
*exprs, **named_exprs
)

if any(returns_scalar):
msg = (
"Expressions which return scalars are not yet supported in `with_columns`\n"
"for the DuckDB backend. Once they introduce duckdb.WindowExpression to \n"
"their Python API, we'll be able to support this."
)
raise NotImplementedError(msg)

result = []
for col in self._native_frame.columns:
if col in new_columns_map:
Expand Down
2 changes: 1 addition & 1 deletion narwhals/_duckdb/expr.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _from_call(
def func(df: DuckDBLazyFrame) -> list[duckdb.Expression]:
native_series_list = self._call(df)
other_native_series = {
key: maybe_evaluate(df, value)
key: maybe_evaluate(df, value, returns_scalar=returns_scalar)
for key, value in expressifiable_args.items()
}
return [
Expand Down
24 changes: 18 additions & 6 deletions narwhals/_duckdb/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from narwhals.utils import Version


def maybe_evaluate(df: DuckDBLazyFrame, obj: Any) -> Any:
def maybe_evaluate(df: DuckDBLazyFrame, obj: Any, *, returns_scalar: bool) -> Any:
from narwhals._duckdb.expr import DuckDBExpr

if isinstance(obj, DuckDBExpr):
Expand All @@ -27,20 +27,30 @@ def maybe_evaluate(df: DuckDBLazyFrame, obj: Any) -> Any:
msg = "Multi-output expressions (e.g. `nw.all()` or `nw.col('a', 'b')`) not supported in this context"
raise NotImplementedError(msg)
column_result = column_results[0]
if obj._returns_scalar:
msg = "Reductions are not yet supported for DuckDB, at least until they implement duckdb.WindowExpression"
if obj._returns_scalar and not returns_scalar:
# Returns scalar, but overall expression doesn't.
# Not yet supported.
msg = (
"Mixing expressions which aggregate and expressions which don't\n"
"is not yet supported by the DuckDB backend. Once they introduce\n"
"duckdb.WindowExpression to their Python API, we'll be able to\n"
"support this."
)
raise NotImplementedError(msg)
return column_result
return duckdb.ConstantExpression(obj)


def parse_exprs_and_named_exprs(
df: DuckDBLazyFrame,
) -> Callable[..., dict[str, duckdb.Expression]]:
) -> Callable[..., tuple[dict[str, duckdb.Expression], list[bool]]]:
def func(
*exprs: DuckDBExpr, **named_exprs: DuckDBExpr
) -> dict[str, duckdb.Expression]:
) -> tuple[dict[str, duckdb.Expression], list[bool]]:
native_results: dict[str, list[duckdb.Expression]] = {}

# `returns_scalar` keeps track if an expression returns a scalar.
returns_scalar: list[bool] = []
for expr in exprs:
native_series_list = expr._call(df)
output_names = expr._evaluate_output_names(df)
Expand All @@ -50,13 +60,15 @@ def func(
msg = f"Internal error: got output names {output_names}, but only got {len(native_series_list)} results"
raise AssertionError(msg)
native_results.update(zip(output_names, native_series_list))
returns_scalar.extend([expr._returns_scalar] * len(output_names))
for col_alias, expr in named_exprs.items():
native_series_list = expr._call(df)
if len(native_series_list) != 1: # pragma: no cover
msg = "Named expressions must return a single column"
raise ValueError(msg)
native_results[col_alias] = native_series_list[0]
return native_results
returns_scalar.append(expr._returns_scalar)
return native_results, returns_scalar

return func

Expand Down
4 changes: 2 additions & 2 deletions narwhals/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1035,11 +1035,11 @@ def generate_repr(header: str, native_repr: str) -> str:
try:
terminal_width = os.get_terminal_size().columns
except OSError:
terminal_width = 80
terminal_width = int(os.getenv("COLUMNS", 80)) # noqa: PLW1508
native_lines = native_repr.splitlines()
max_native_width = max(len(line) for line in native_lines)

if max_native_width + 2 < terminal_width:
if max_native_width + 2 <= terminal_width:
Comment on lines -1038 to +1042
Copy link
Member Author

@MarcoGorelli MarcoGorelli Jan 27, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

drive-by, but i noticed that on kaggle notebooks os.get_terminal_size() raises, but COLUMNS is set to 100. COLUMNS seems to be the IPython standard

length = max(max_native_width, len(header))
output = f"β”Œ{'─'*length}┐\n"
header_extra = length - len(header)
Expand Down
5 changes: 0 additions & 5 deletions tests/expr_and_series/lit_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,6 @@ def test_lit_operation_in_select(
if "duckdb" in str(constructor) and col_name in (
"left_scalar_with_agg",
"left_lit_with_agg",
"right_lit",
"right_lit_with_agg",
):
request.applymarker(pytest.mark.xfail)
if (
Expand Down Expand Up @@ -126,10 +124,7 @@ def test_lit_operation_in_with_columns(
col_name: str,
expr: nw.Expr,
expected_result: list[int],
request: pytest.FixtureRequest,
) -> None:
if "duckdb" in str(constructor) and col_name == "scalar_and_lit":
request.applymarker(pytest.mark.xfail)
data = {"a": [1, 3, 2]}
df_raw = constructor(data)
df = nw.from_native(df_raw).lazy()
Expand Down
2 changes: 1 addition & 1 deletion tpch/execute.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"dask": lambda x: x.compute(),
}

DUCKDB_SKIPS = ["q14", "q15"]
DUCKDB_SKIPS = ["q15"]
MarcoGorelli marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just one left!

πŸ₯³


QUERY_DATA_PATH_MAP = {
"q1": (LINEITEM_PATH,),
Expand Down
Loading