Skip to content

Commit c3eef9d

Browse files
authored
chore: rename _input to expr in _duckdb (#2580)
chore: _input -> expr
1 parent c7bc710 commit c3eef9d

File tree

5 files changed

+128
-137
lines changed

5 files changed

+128
-137
lines changed

narwhals/_duckdb/expr.py

Lines changed: 81 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -290,95 +290,93 @@ def _alias_native(cls, expr: Expression, name: str) -> Expression:
290290
return expr.alias(name)
291291

292292
def __and__(self, other: DuckDBExpr) -> Self:
293-
return self._with_callable(lambda _input, other: _input & other, other=other)
293+
return self._with_callable(lambda expr, other: expr & other, other=other)
294294

295295
def __or__(self, other: DuckDBExpr) -> Self:
296-
return self._with_callable(lambda _input, other: _input | other, other=other)
296+
return self._with_callable(lambda expr, other: expr | other, other=other)
297297

298298
def __add__(self, other: DuckDBExpr) -> Self:
299-
return self._with_callable(lambda _input, other: _input + other, other=other)
299+
return self._with_callable(lambda expr, other: expr + other, other=other)
300300

301301
def __truediv__(self, other: DuckDBExpr) -> Self:
302-
return self._with_callable(lambda _input, other: _input / other, other=other)
302+
return self._with_callable(lambda expr, other: expr / other, other=other)
303303

304304
def __rtruediv__(self, other: DuckDBExpr) -> Self:
305305
return self._with_callable(
306-
lambda _input, other: other.__truediv__(_input), other=other
306+
lambda expr, other: other.__truediv__(expr), other=other
307307
).alias("literal")
308308

309309
def __floordiv__(self, other: DuckDBExpr) -> Self:
310310
return self._with_callable(
311-
lambda _input, other: _input.__floordiv__(other), other=other
311+
lambda expr, other: expr.__floordiv__(other), other=other
312312
)
313313

314314
def __rfloordiv__(self, other: DuckDBExpr) -> Self:
315315
return self._with_callable(
316-
lambda _input, other: other.__floordiv__(_input), other=other
316+
lambda expr, other: other.__floordiv__(expr), other=other
317317
).alias("literal")
318318

319319
def __mod__(self, other: DuckDBExpr) -> Self:
320-
return self._with_callable(
321-
lambda _input, other: _input.__mod__(other), other=other
322-
)
320+
return self._with_callable(lambda expr, other: expr.__mod__(other), other=other)
323321

324322
def __rmod__(self, other: DuckDBExpr) -> Self:
325323
return self._with_callable(
326-
lambda _input, other: other.__mod__(_input), other=other
324+
lambda expr, other: other.__mod__(expr), other=other
327325
).alias("literal")
328326

329327
def __sub__(self, other: DuckDBExpr) -> Self:
330-
return self._with_callable(lambda _input, other: _input - other, other=other)
328+
return self._with_callable(lambda expr, other: expr - other, other=other)
331329

332330
def __rsub__(self, other: DuckDBExpr) -> Self:
333331
return self._with_callable(
334-
lambda _input, other: other.__sub__(_input), other=other
332+
lambda expr, other: other.__sub__(expr), other=other
335333
).alias("literal")
336334

337335
def __mul__(self, other: DuckDBExpr) -> Self:
338-
return self._with_callable(lambda _input, other: _input * other, other=other)
336+
return self._with_callable(lambda expr, other: expr * other, other=other)
339337

340338
def __pow__(self, other: DuckDBExpr) -> Self:
341-
return self._with_callable(lambda _input, other: _input**other, other=other)
339+
return self._with_callable(lambda expr, other: expr**other, other=other)
342340

343341
def __rpow__(self, other: DuckDBExpr) -> Self:
344342
return self._with_callable(
345-
lambda _input, other: other.__pow__(_input), other=other
343+
lambda expr, other: other.__pow__(expr), other=other
346344
).alias("literal")
347345

348346
def __lt__(self, other: DuckDBExpr) -> Self:
349-
return self._with_callable(lambda _input, other: _input < other, other=other)
347+
return self._with_callable(lambda expr, other: expr < other, other=other)
350348

351349
def __gt__(self, other: DuckDBExpr) -> Self:
352-
return self._with_callable(lambda _input, other: _input > other, other=other)
350+
return self._with_callable(lambda expr, other: expr > other, other=other)
353351

354352
def __le__(self, other: DuckDBExpr) -> Self:
355-
return self._with_callable(lambda _input, other: _input <= other, other=other)
353+
return self._with_callable(lambda expr, other: expr <= other, other=other)
356354

357355
def __ge__(self, other: DuckDBExpr) -> Self:
358-
return self._with_callable(lambda _input, other: _input >= other, other=other)
356+
return self._with_callable(lambda expr, other: expr >= other, other=other)
359357

360358
def __eq__(self, other: DuckDBExpr) -> Self: # type: ignore[override]
361-
return self._with_callable(lambda _input, other: _input == other, other=other)
359+
return self._with_callable(lambda expr, other: expr == other, other=other)
362360

363361
def __ne__(self, other: DuckDBExpr) -> Self: # type: ignore[override]
364-
return self._with_callable(lambda _input, other: _input != other, other=other)
362+
return self._with_callable(lambda expr, other: expr != other, other=other)
365363

366364
def __invert__(self) -> Self:
367365
invert = cast("Callable[..., Expression]", operator.invert)
368366
return self._with_callable(invert)
369367

370368
def abs(self) -> Self:
371-
return self._with_callable(lambda _input: FunctionExpression("abs", _input))
369+
return self._with_callable(lambda expr: FunctionExpression("abs", expr))
372370

373371
def mean(self) -> Self:
374-
return self._with_callable(lambda _input: FunctionExpression("mean", _input))
372+
return self._with_callable(lambda expr: FunctionExpression("mean", expr))
375373

376374
def skew(self) -> Self:
377-
def func(_input: Expression) -> Expression:
378-
count = FunctionExpression("count", _input)
375+
def func(expr: Expression) -> Expression:
376+
count = FunctionExpression("count", expr)
379377
# Adjust population skewness by correction factor to get sample skewness
380378
sample_skewness = (
381-
FunctionExpression("skewness", _input)
379+
FunctionExpression("skewness", expr)
382380
* (count - lit(2))
383381
/ FunctionExpression("sqrt", count * (count - lit(1)))
384382
)
@@ -391,20 +389,20 @@ def func(_input: Expression) -> Expression:
391389
return self._with_callable(func)
392390

393391
def median(self) -> Self:
394-
return self._with_callable(lambda _input: FunctionExpression("median", _input))
392+
return self._with_callable(lambda expr: FunctionExpression("median", expr))
395393

396394
def all(self) -> Self:
397-
return self._with_callable(lambda _input: FunctionExpression("bool_and", _input))
395+
return self._with_callable(lambda expr: FunctionExpression("bool_and", expr))
398396

399397
def any(self) -> Self:
400-
return self._with_callable(lambda _input: FunctionExpression("bool_or", _input))
398+
return self._with_callable(lambda expr: FunctionExpression("bool_or", expr))
401399

402400
def quantile(
403401
self, quantile: float, interpolation: RollingInterpolationMethod
404402
) -> Self:
405-
def func(_input: Expression) -> Expression:
403+
def func(expr: Expression) -> Expression:
406404
if interpolation == "linear":
407-
return FunctionExpression("quantile_cont", _input, lit(quantile))
405+
return FunctionExpression("quantile_cont", expr, lit(quantile))
408406
msg = "Only linear interpolation methods are supported for DuckDB quantile."
409407
raise NotImplementedError(msg)
410408

@@ -415,17 +413,17 @@ def clip(
415413
lower_bound: Self | NumericLiteral | TemporalLiteral | None,
416414
upper_bound: Self | NumericLiteral | TemporalLiteral | None,
417415
) -> Self:
418-
def _clip_lower(_input: Expression, lower_bound: Any) -> Expression:
419-
return FunctionExpression("greatest", _input, lower_bound)
416+
def _clip_lower(expr: Expression, lower_bound: Any) -> Expression:
417+
return FunctionExpression("greatest", expr, lower_bound)
420418

421-
def _clip_upper(_input: Expression, upper_bound: Any) -> Expression:
422-
return FunctionExpression("least", _input, upper_bound)
419+
def _clip_upper(expr: Expression, upper_bound: Any) -> Expression:
420+
return FunctionExpression("least", expr, upper_bound)
423421

424422
def _clip_both(
425-
_input: Expression, lower_bound: Any, upper_bound: Any
423+
expr: Expression, lower_bound: Any, upper_bound: Any
426424
) -> Expression:
427425
return FunctionExpression(
428-
"greatest", FunctionExpression("least", _input, upper_bound), lower_bound
426+
"greatest", FunctionExpression("least", expr, upper_bound), lower_bound
429427
)
430428

431429
if lower_bound is None:
@@ -437,39 +435,39 @@ def _clip_both(
437435
)
438436

439437
def sum(self) -> Self:
440-
return self._with_callable(lambda _input: FunctionExpression("sum", _input))
438+
return self._with_callable(lambda expr: FunctionExpression("sum", expr))
441439

442440
def n_unique(self) -> Self:
443-
def func(_input: Expression) -> Expression:
441+
def func(expr: Expression) -> Expression:
444442
# https://stackoverflow.com/a/79338887/4451315
445443
return FunctionExpression(
446-
"array_unique", FunctionExpression("array_agg", _input)
444+
"array_unique", FunctionExpression("array_agg", expr)
447445
) + FunctionExpression(
448-
"max", when(_input.isnotnull(), lit(0)).otherwise(lit(1))
446+
"max", when(expr.isnotnull(), lit(0)).otherwise(lit(1))
449447
)
450448

451449
return self._with_callable(func)
452450

453451
def count(self) -> Self:
454-
return self._with_callable(lambda _input: FunctionExpression("count", _input))
452+
return self._with_callable(lambda expr: FunctionExpression("count", expr))
455453

456454
def len(self) -> Self:
457-
return self._with_callable(lambda _input: FunctionExpression("count"))
455+
return self._with_callable(lambda _expr: FunctionExpression("count"))
458456

459457
def std(self, ddof: int) -> Self:
460458
if ddof == 0:
461459
return self._with_callable(
462-
lambda _input: FunctionExpression("stddev_pop", _input)
460+
lambda expr: FunctionExpression("stddev_pop", expr)
463461
)
464462
if ddof == 1:
465463
return self._with_callable(
466-
lambda _input: FunctionExpression("stddev_samp", _input)
464+
lambda expr: FunctionExpression("stddev_samp", expr)
467465
)
468466

469-
def _std(_input: Expression) -> Expression:
470-
n_samples = FunctionExpression("count", _input)
467+
def _std(expr: Expression) -> Expression:
468+
n_samples = FunctionExpression("count", expr)
471469
return (
472-
FunctionExpression("stddev_pop", _input)
470+
FunctionExpression("stddev_pop", expr)
473471
* FunctionExpression("sqrt", n_samples)
474472
/ (FunctionExpression("sqrt", (n_samples - lit(ddof))))
475473
)
@@ -478,33 +476,27 @@ def _std(_input: Expression) -> Expression:
478476

479477
def var(self, ddof: int) -> Self:
480478
if ddof == 0:
481-
return self._with_callable(
482-
lambda _input: FunctionExpression("var_pop", _input)
483-
)
479+
return self._with_callable(lambda expr: FunctionExpression("var_pop", expr))
484480
if ddof == 1:
485-
return self._with_callable(
486-
lambda _input: FunctionExpression("var_samp", _input)
487-
)
481+
return self._with_callable(lambda expr: FunctionExpression("var_samp", expr))
488482

489-
def _var(_input: Expression) -> Expression:
490-
n_samples = FunctionExpression("count", _input)
483+
def _var(expr: Expression) -> Expression:
484+
n_samples = FunctionExpression("count", expr)
491485
return (
492-
FunctionExpression("var_pop", _input)
493-
* n_samples
494-
/ (n_samples - lit(ddof))
486+
FunctionExpression("var_pop", expr) * n_samples / (n_samples - lit(ddof))
495487
)
496488

497489
return self._with_callable(_var)
498490

499491
def max(self) -> Self:
500-
return self._with_callable(lambda _input: FunctionExpression("max", _input))
492+
return self._with_callable(lambda expr: FunctionExpression("max", expr))
501493

502494
def min(self) -> Self:
503-
return self._with_callable(lambda _input: FunctionExpression("min", _input))
495+
return self._with_callable(lambda expr: FunctionExpression("min", expr))
504496

505497
def null_count(self) -> Self:
506498
return self._with_callable(
507-
lambda _input: FunctionExpression("sum", _input.isnull().cast("int")),
499+
lambda expr: FunctionExpression("sum", expr.isnull().cast("int")),
508500
)
509501

510502
@requires.backend_version((1, 3))
@@ -548,22 +540,22 @@ def func(df: DuckDBLazyFrame) -> list[Expression]:
548540
)
549541

550542
def is_null(self) -> Self:
551-
return self._with_callable(lambda _input: _input.isnull())
543+
return self._with_callable(lambda expr: expr.isnull())
552544

553545
def is_nan(self) -> Self:
554-
return self._with_callable(lambda _input: FunctionExpression("isnan", _input))
546+
return self._with_callable(lambda expr: FunctionExpression("isnan", expr))
555547

556548
def is_finite(self) -> Self:
557-
return self._with_callable(lambda _input: FunctionExpression("isfinite", _input))
549+
return self._with_callable(lambda expr: FunctionExpression("isfinite", expr))
558550

559551
def is_in(self, other: Sequence[Any]) -> Self:
560552
return self._with_callable(
561-
lambda _input: FunctionExpression("contains", lit(other), _input)
553+
lambda expr: FunctionExpression("contains", lit(other), expr)
562554
)
563555

564556
def round(self, decimals: int) -> Self:
565557
return self._with_callable(
566-
lambda _input: FunctionExpression("round", _input, lit(decimals))
558+
lambda expr: FunctionExpression("round", expr, lit(decimals))
567559
)
568560

569561
@requires.backend_version((1, 3))
@@ -734,22 +726,22 @@ def _fill_with_strategy(window_inputs: WindowInputs) -> Expression:
734726

735727
return self._with_window_function(_fill_with_strategy)
736728

737-
def _fill_constant(_input: Expression, value: Any) -> Expression:
738-
return CoalesceOperator(_input, value)
729+
def _fill_constant(expr: Expression, value: Any) -> Expression:
730+
return CoalesceOperator(expr, value)
739731

740732
return self._with_callable(_fill_constant, value=value)
741733

742734
def cast(self, dtype: DType | type[DType]) -> Self:
743-
def func(_input: Expression) -> Expression:
735+
def func(expr: Expression) -> Expression:
744736
native_dtype = narwhals_to_native_dtype(dtype, self._version)
745-
return _input.cast(DuckDBPyType(native_dtype))
737+
return expr.cast(DuckDBPyType(native_dtype))
746738

747739
return self._with_callable(func)
748740

749741
@requires.backend_version((1, 3))
750742
def is_unique(self) -> Self:
751-
def func(_input: Expression) -> Expression:
752-
sql = f"count(*) over (partition by {_input})"
743+
def func(expr: Expression) -> Expression:
744+
sql = f"count(*) over (partition by {expr})"
753745
return SQLExpression(sql) == lit(1) # type: ignore[no-any-return, unused-ignore]
754746

755747
return self._with_callable(func)
@@ -764,39 +756,39 @@ def rank(self, method: RankMethod, *, descending: bool) -> Self:
764756
func = FunctionExpression("row_number")
765757

766758
def _rank(
767-
_input: Expression,
759+
expr: Expression,
768760
*,
769761
descending: bool,
770762
partition_by: Sequence[str | Expression] | None = None,
771763
) -> Expression:
772764
order_by_sql = (
773-
f"order by {_input} desc nulls last"
765+
f"order by {expr} desc nulls last"
774766
if descending
775-
else f"order by {_input} asc nulls last"
767+
else f"order by {expr} asc nulls last"
776768
)
777769
count_expr = FunctionExpression("count", StarExpression())
778770
if partition_by is not None:
779771
window = f"{generate_partition_by_sql(*partition_by)} {order_by_sql}"
780-
count_window = f"{generate_partition_by_sql(*partition_by, _input)}"
772+
count_window = f"{generate_partition_by_sql(*partition_by, expr)}"
781773
else:
782774
window = order_by_sql
783-
count_window = generate_partition_by_sql(_input)
775+
count_window = generate_partition_by_sql(expr)
784776
if method == "max":
785-
expr = (
777+
rank_expr = (
786778
SQLExpression(f"{func} OVER ({window})")
787779
+ SQLExpression(f"{count_expr} over ({count_window})")
788780
- lit(1)
789781
)
790782
elif method == "average":
791-
expr = SQLExpression(f"{func} OVER ({window})") + (
783+
rank_expr = SQLExpression(f"{func} OVER ({window})") + (
792784
SQLExpression(f"{count_expr} over ({count_window})") - lit(1)
793785
) / lit(2.0)
794786
else:
795-
expr = SQLExpression(f"{func} OVER ({window})")
796-
return when(_input.isnotnull(), expr)
787+
rank_expr = SQLExpression(f"{func} OVER ({window})")
788+
return when(expr.isnotnull(), rank_expr)
797789

798-
def _unpartitioned_rank(_input: Expression) -> Expression:
799-
return _rank(_input, descending=descending)
790+
def _unpartitioned_rank(expr: Expression) -> Expression:
791+
return _rank(expr, descending=descending)
800792

801793
def _partitioned_rank(
802794
window_inputs: UnorderableWindowInputs,
@@ -813,11 +805,11 @@ def _partitioned_rank(
813805
)
814806

815807
def log(self, base: float) -> Self:
816-
def _log(_input: Expression) -> Expression:
817-
log = FunctionExpression("log", _input)
808+
def _log(expr: Expression) -> Expression:
809+
log = FunctionExpression("log", expr)
818810
return (
819-
when(_input < lit(0), lit(float("nan")))
820-
.when(_input == lit(0), lit(float("-inf")))
811+
when(expr < lit(0), lit(float("nan")))
812+
.when(expr == lit(0), lit(float("-inf")))
821813
.otherwise(log / FunctionExpression("log", lit(base)))
822814
)
823815

0 commit comments

Comments
 (0)