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

Conversation

MarcoGorelli
Copy link
Member

@MarcoGorelli MarcoGorelli commented Jan 27, 2025

Very similar to the recent PySpark PR #1870

What type of PR is this? (check all applicable)

  • πŸ’Ύ Refactor
  • ✨ Feature
  • πŸ› Bug Fix
  • πŸ”§ Optimization
  • πŸ“ Documentation
  • βœ… Test
  • 🐳 Other

Related issues

  • Related issue #<issue number>
  • Closes #<issue number>

Checklist

  • Code follows style guide (ruff)
  • Tests added
  • Documented the changes

If you have comments or can explain your changes, please do so below

Comment on lines -1038 to +1042
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:
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

@MarcoGorelli MarcoGorelli marked this pull request as ready for review January 27, 2025 19:44
@MarcoGorelli MarcoGorelli added the enhancement New feature or request label Jan 27, 2025
@MarcoGorelli MarcoGorelli marked this pull request as draft January 27, 2025 19:58
@MarcoGorelli
Copy link
Member Author

uurghh

gonna have to come back to this

@MarcoGorelli
Copy link
Member Author

πŸ€” we might need _is_literal 😭

@MarcoGorelli
Copy link
Member Author

just jotting down some thoughts:

  • by "aggregation", I mean both nw.col('a').mean() and nw.lit(1)
  • literal aggregation: nw.lit(1)
  • non-literal aggregation: nw.col('a').mean()
  • non-aggregation: nw.col('a').round()

n-ary operation between expressions:

  • if there's at least one non-literal aggregation and at least one non-aggregation, then the aggregation needs a "over ()"
  • in all other cases, leave the expressions as they are
  • if all the expressions are literal aggregations, then the output is a literal aggregation
  • if all expressions aggregate, then the output expressions also aggregates
  • in all other cases, the output doesn't aggregate

select:

  • if all expressions aggregate or are literals, use agg
  • in all other cases, use .select. broadcasting should already have happened by this stage

with_columns:

  • aggregations which aren't literals need an "over ()"

@MarcoGorelli MarcoGorelli marked this pull request as ready for review January 27, 2025 22:38
DUCKDB_SKIPS = ["q14", "q15"]
DUCKDB_SKIPS = ["q15"]
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!

πŸ₯³

Copy link
Member

@FBruzzesi FBruzzesi left a comment

Choose a reason for hiding this comment

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

Amazing 🀩 I think this approach will be very useful elsewhere πŸ˜‰

Let a tiny suggestion on ExprKind docstring, feel free to expand on it

WelcomeDealWithItGIF

# it means that it was a scalar (e.g. nw.col('a') + 1), and so we default
# to `True`.
return lhs._returns_scalar and getattr(rhs, "_returns_scalar", True)
def n_ary_operation_expr_kind(*args: DuckDBExpr | Any) -> ExprKind:
Copy link
Member

Choose a reason for hiding this comment

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

Nice one!

Comment on lines 23 to 24
class ExprKind(Enum):
LITERAL = auto() # e.g. nw.lit(1)
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth adding a tiny docstring and describe interaction between values:

Suggested change
class ExprKind(Enum):
LITERAL = auto() # e.g. nw.lit(1)
class ExprKind(Enum):
"""Describe which kind of expression we are dealing with.
Composition rule is:
- LITERAL & LITERAL -> LITERAL
- TRANSFORM & X -> TRANSFORM
- X & TRANSFORM -> TRANSFORM
- all remaining cases -> AGGREGATION
"""
LITERAL = auto() # e.g. nw.lit(1)

@MarcoGorelli
Copy link
Member Author

thanks for your review!

@MarcoGorelli MarcoGorelli merged commit b4ff6a0 into narwhals-dev:main Jan 28, 2025
22 of 23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants