-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pyspark and duckdb
Expr.name
namespace (#1809)
* feat: pyspark name namespace * duckdb * use anonymousexprerror * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: Marco Edward Gorelli <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
fb1d34e
commit 0bcb500
Showing
11 changed files
with
327 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Callable | ||
|
||
from narwhals.exceptions import AnonymousExprError | ||
|
||
if TYPE_CHECKING: | ||
from typing_extensions import Self | ||
|
||
from narwhals._duckdb.expr import DuckDBExpr | ||
|
||
|
||
class DuckDBExprNameNamespace: | ||
def __init__(self: Self, expr: DuckDBExpr) -> None: | ||
self._compliant_expr = expr | ||
|
||
def keep(self: Self) -> DuckDBExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.keep" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), root_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=root_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs=self._compliant_expr._kwargs, | ||
) | ||
|
||
def map(self: Self, function: Callable[[str], str]) -> DuckDBExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.map" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [function(str(name)) for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs={**self._compliant_expr._kwargs, "function": function}, | ||
) | ||
|
||
def prefix(self: Self, prefix: str) -> DuckDBExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.prefix" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [prefix + str(name) for name in root_names] | ||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs={**self._compliant_expr._kwargs, "prefix": prefix}, | ||
) | ||
|
||
def suffix(self: Self, suffix: str) -> DuckDBExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.suffix" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [str(name) + suffix for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs={**self._compliant_expr._kwargs, "suffix": suffix}, | ||
) | ||
|
||
def to_lowercase(self: Self) -> DuckDBExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.to_lowercase" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [str(name).lower() for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs=self._compliant_expr._kwargs, | ||
) | ||
|
||
def to_uppercase(self: Self) -> DuckDBExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.to_uppercase" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
output_names = [str(name).upper() for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs=self._compliant_expr._kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from typing import Callable | ||
|
||
from narwhals.exceptions import AnonymousExprError | ||
|
||
if TYPE_CHECKING: | ||
from typing_extensions import Self | ||
|
||
from narwhals._spark_like.expr import SparkLikeExpr | ||
|
||
|
||
class SparkLikeExprNameNamespace: | ||
def __init__(self: Self, expr: SparkLikeExpr) -> None: | ||
self._compliant_expr = expr | ||
|
||
def keep(self: Self) -> SparkLikeExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.keep" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), root_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=root_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs=self._compliant_expr._kwargs, | ||
) | ||
|
||
def map(self: Self, function: Callable[[str], str]) -> SparkLikeExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.map" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [function(str(name)) for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs={**self._compliant_expr._kwargs, "function": function}, | ||
) | ||
|
||
def prefix(self: Self, prefix: str) -> SparkLikeExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.prefix" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [prefix + str(name) for name in root_names] | ||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs={**self._compliant_expr._kwargs, "prefix": prefix}, | ||
) | ||
|
||
def suffix(self: Self, suffix: str) -> SparkLikeExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.suffix" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
|
||
output_names = [str(name) + suffix for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs={**self._compliant_expr._kwargs, "suffix": suffix}, | ||
) | ||
|
||
def to_lowercase(self: Self) -> SparkLikeExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.to_lowercase" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
output_names = [str(name).lower() for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs=self._compliant_expr._kwargs, | ||
) | ||
|
||
def to_uppercase(self: Self) -> SparkLikeExpr: | ||
root_names = self._compliant_expr._root_names | ||
if root_names is None: | ||
msg = ".name.to_uppercase" | ||
raise AnonymousExprError.from_expr_name(msg) | ||
output_names = [str(name).upper() for name in root_names] | ||
|
||
return self._compliant_expr.__class__( | ||
lambda df: [ | ||
expr.alias(name) | ||
for expr, name in zip(self._compliant_expr._call(df), output_names) | ||
], | ||
depth=self._compliant_expr._depth, | ||
function_name=self._compliant_expr._function_name, | ||
root_names=root_names, | ||
output_names=output_names, | ||
returns_scalar=self._compliant_expr._returns_scalar, | ||
backend_version=self._compliant_expr._backend_version, | ||
version=self._compliant_expr._version, | ||
kwargs=self._compliant_expr._kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.