-
Notifications
You must be signed in to change notification settings - Fork 17
feat: narwhals-compliant dataframe setup #112
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,17 @@ | ||
| from substrait.builders.plan import read_named_table | ||
| from substrait.builders.type import i64, boolean, struct, named_struct | ||
| from substrait.extension_registry import ExtensionRegistry | ||
| import substrait.dataframe as sdf | ||
|
|
||
| registry = ExtensionRegistry(load_default_extensions=True) | ||
|
|
||
| ns = named_struct( | ||
| names=["id", "is_applicable"], | ||
| struct=struct(types=[i64(nullable=False), boolean()], nullable=False), | ||
| ) | ||
|
|
||
| table = read_named_table("example_table", ns) | ||
|
|
||
| frame = sdf.DataFrame(read_named_table("example_table", ns)) | ||
| frame = frame.select(sdf.col("id")) | ||
| print(frame.to_substrait(registry)) |
This file contains hidden or 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,36 @@ | ||
| # Install duckdb and pyarrow before running this example | ||
| # /// script | ||
| # dependencies = [ | ||
| # "narwhals==2.9.0", | ||
| # "substrait[extensions] @ file:///${PROJECT_ROOT}/" | ||
| # ] | ||
| # /// | ||
|
|
||
| from substrait.builders.plan import read_named_table | ||
| from substrait.builders.type import i64, boolean, struct, named_struct | ||
| from substrait.extension_registry import ExtensionRegistry | ||
|
|
||
| from narwhals.typing import FrameT | ||
| import narwhals as nw | ||
| import substrait.dataframe as sdf | ||
|
|
||
|
|
||
| registry = ExtensionRegistry(load_default_extensions=True) | ||
|
|
||
| ns = named_struct( | ||
| names=["id", "is_applicable"], | ||
| struct=struct(types=[i64(nullable=False), boolean()], nullable=False), | ||
| ) | ||
|
|
||
| table = read_named_table("example_table", ns) | ||
|
|
||
|
|
||
| lazy_frame: FrameT = nw.from_native( | ||
| sdf.DataFrame(read_named_table("example_table", ns)) | ||
| ) | ||
|
|
||
| lazy_frame = lazy_frame.select(nw.col("id").abs(), new_id=nw.col("id")) | ||
|
|
||
| df: sdf.DataFrame = lazy_frame.to_native() | ||
|
|
||
| print(df.to_substrait(registry)) |
This file contains hidden or 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,16 @@ | ||
| import substrait.dataframe | ||
| from substrait.builders.extended_expression import column | ||
|
|
||
| from substrait.dataframe.dataframe import DataFrame | ||
| from substrait.dataframe.expression import Expression | ||
|
|
||
| __all__ = [DataFrame, Expression] | ||
|
|
||
|
|
||
| def col(name: str) -> Expression: | ||
| """Column selection.""" | ||
| return Expression(column(name)) | ||
|
|
||
| # TODO handle str_as_lit argument | ||
| def parse_into_expr(expr, str_as_lit: bool): | ||
| return expr._to_compliant_expr(substrait.dataframe) |
This file contains hidden or 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,36 @@ | ||
| from typing import Union, Iterable | ||
| import substrait.dataframe | ||
| from substrait.builders.plan import select | ||
| from substrait.dataframe.expression import Expression | ||
|
|
||
|
|
||
| class DataFrame: | ||
| def __init__(self, plan): | ||
| self.plan = plan | ||
| self._native_frame = self | ||
|
|
||
| def to_substrait(self, registry): | ||
| return self.plan(registry) | ||
|
|
||
| def __narwhals_lazyframe__(self) -> "DataFrame": | ||
| """Return object implementing CompliantDataFrame protocol.""" | ||
| return self | ||
|
|
||
| def __narwhals_namespace__(self): | ||
| """ | ||
| Return the namespace object that contains functions like col, lit, etc. | ||
| This is how Narwhals knows which backend's functions to use. | ||
| """ | ||
| return substrait.dataframe | ||
|
|
||
| def select( | ||
| self, *exprs: Union[Expression, Iterable[Expression]], **named_exprs: Expression | ||
| ) -> "DataFrame": | ||
| expressions = [e.expr for e in exprs] + [ | ||
| expr.alias(alias).expr for alias, expr in named_exprs.items() | ||
| ] | ||
| return DataFrame(select(self.plan, expressions=expressions)) | ||
|
|
||
| # TODO handle version | ||
| def _with_version(self, version): | ||
| return self | ||
This file contains hidden or 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,36 @@ | ||
| from substrait.builders.extended_expression import ( | ||
| UnboundExtendedExpression, | ||
| ExtendedExpressionOrUnbound, | ||
| resolve_expression, | ||
| scalar_function | ||
| ) | ||
| import substrait.gen.proto.type_pb2 as stp | ||
| import substrait.gen.proto.extended_expression_pb2 as stee | ||
| from substrait.extension_registry import ExtensionRegistry | ||
|
|
||
|
|
||
| def _alias( | ||
| expr: ExtendedExpressionOrUnbound, | ||
| alias: str = None, | ||
| ): | ||
| def resolve( | ||
| base_schema: stp.NamedStruct, registry: ExtensionRegistry | ||
| ) -> stee.ExtendedExpression: | ||
| bound_expression = resolve_expression(expr, base_schema, registry) | ||
| bound_expression.referred_expr[0].output_names[0] = alias | ||
| return bound_expression | ||
|
|
||
| return resolve | ||
|
|
||
|
|
||
| class Expression: | ||
| def __init__(self, expr: UnboundExtendedExpression): | ||
| self.expr = expr | ||
|
|
||
| def alias(self, alias: str): | ||
| self.expr = _alias(self.expr, alias) | ||
| return self | ||
|
|
||
| def abs(self): | ||
| self.expr = scalar_function("functions_arithmetic.yaml", "abs", expressions=[self.expr]) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suspect this PR will get merged before your work in #107. We'll need to make sure to update this. |
||
| return self | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's needs to exist to handle this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no clue at this point, I need to dig deeper to understand if we can disregard it or not. that's what I meant by "handle version".