-
Notifications
You must be signed in to change notification settings - Fork 414
[Do not merge] Iterative bind with a stack instead of recursion
#1783
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
Closed
Closed
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,103 @@ | ||
| from functools import singledispatch | ||
| from typing import ( | ||
| List, | ||
| Tuple, | ||
| TypeVar, | ||
| ) | ||
|
|
||
| from pyiceberg.expressions import ( | ||
| AlwaysFalse, | ||
| AlwaysTrue, | ||
| And, | ||
| BooleanExpression, | ||
| BoundPredicate, | ||
| Not, | ||
| Or, | ||
| UnboundPredicate, | ||
| ) | ||
| from pyiceberg.expressions.visitors import BindVisitor, BooleanExpressionVisitor | ||
| from pyiceberg.schema import Schema | ||
| from pyiceberg.typedef import L | ||
|
|
||
| T = TypeVar("T") | ||
|
|
||
|
|
||
| @singledispatch | ||
| def _visit_stack(obj: BooleanExpression, stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| raise NotImplementedError(f"Cannot visit unsupported expression: {obj}") | ||
|
|
||
|
|
||
| @_visit_stack.register(AlwaysTrue) | ||
| def _(_: AlwaysTrue, stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| stack.append(visitor.visit_true()) | ||
|
|
||
|
|
||
| @_visit_stack.register(AlwaysFalse) | ||
| def _(_: AlwaysFalse, stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| stack.append(visitor.visit_false()) | ||
|
|
||
|
|
||
| @_visit_stack.register(Not) | ||
| def _(_: Not, stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| child_result = stack.pop() | ||
| stack.append(visitor.visit_not(child_result)) | ||
|
|
||
|
|
||
| @_visit_stack.register(And) | ||
| def _(_: And, stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| right_result = stack.pop() | ||
| left_result = stack.pop() | ||
| stack.append(visitor.visit_and(left_result, right_result)) | ||
|
|
||
|
|
||
| @_visit_stack.register(UnboundPredicate) | ||
| def _(obj: UnboundPredicate[L], stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| stack.append(visitor.visit_unbound_predicate(predicate=obj)) | ||
|
|
||
|
|
||
| @_visit_stack.register(BoundPredicate) | ||
| def _(obj: BoundPredicate[L], stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| stack.append(visitor.visit_bound_predicate(predicate=obj)) | ||
|
|
||
|
|
||
| @_visit_stack.register(Or) | ||
| def _(_: Or, stack: List[T], visitor: BooleanExpressionVisitor[T]) -> None: | ||
| right_result = stack.pop() | ||
| left_result = stack.pop() | ||
| stack.append(visitor.visit_or(left_result, right_result)) | ||
|
|
||
|
|
||
| def visit_iterative(expression: BooleanExpression, visitor: BooleanExpressionVisitor[T]) -> T: | ||
| # Store (node, visited) pairs in the stack of expressions to process | ||
| stack: List[Tuple[BooleanExpression, bool]] = [(expression, False)] | ||
| # Store the results of the visit in another stack | ||
| results_stack: List[T] = [] | ||
|
|
||
| while stack: | ||
| node, visited = stack.pop() | ||
| if not visited: | ||
| stack.append((node, True)) | ||
| # TODO: Make this nicer. | ||
| if isinstance(node, Not): | ||
| stack.append((node.child, False)) | ||
| elif isinstance(node, And) or isinstance(node, Or): | ||
| stack.append((node.right, False)) | ||
| stack.append((node.left, False)) | ||
| else: | ||
| _visit_stack(node, results_stack, visitor) | ||
|
|
||
| return results_stack.pop() | ||
|
|
||
|
|
||
| def bind_iterative(schema: Schema, expression: BooleanExpression, case_sensitive: bool) -> BooleanExpression: | ||
| """Traverse iteratively over an expression to bind the predicates to the schema. | ||
|
|
||
| Args: | ||
| schema (Schema): A schema to use when binding the expression. | ||
| expression (BooleanExpression): An expression containing UnboundPredicates that can be bound. | ||
| case_sensitive (bool): Whether to consider case when binding a reference to a field in a schema, defaults to True. | ||
|
|
||
| Raises: | ||
| TypeError: In the case a predicate is already bound. | ||
| """ | ||
| return visit_iterative(expression, BindVisitor(schema, case_sensitive)) |
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
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
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
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 |
|---|---|---|
|
|
@@ -408,6 +408,31 @@ def test_upsert_into_empty_table(catalog: Catalog) -> None: | |
| assert upd.rows_inserted == 4 | ||
|
|
||
|
|
||
| def test_large_upsert_into_empty_table(catalog: Catalog) -> None: | ||
| identifier = "default.test_upsert_large_table" | ||
| _drop_table(catalog, identifier) | ||
|
|
||
| num_columns = 50 | ||
| num_rows = 10000 | ||
|
Comment on lines
+415
to
+416
Contributor
Author
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. Actually, 20 and 1000 is enough to make |
||
|
|
||
| schema = Schema( | ||
| *[NestedField(i, f"field_{i}", StringType(), required=True) for i in range(1, num_columns + 1)], | ||
| identifier_field_ids=[1, 2], | ||
| ) | ||
|
|
||
| tbl = catalog.create_table(identifier, schema=schema) | ||
|
|
||
| arrow_schema = pa.schema([pa.field(f"field_{i}", pa.string(), nullable=False) for i in range(1, num_columns + 1)]) | ||
|
|
||
| data = [{f"field_{i}": f"value_{i}_{j}" for i in range(1, num_columns + 1)} for j in range(num_rows)] | ||
|
|
||
| df = pa.Table.from_pylist(data, schema=arrow_schema) | ||
| upd = tbl.upsert(df) | ||
|
|
||
| assert upd.rows_updated == 0 | ||
| assert upd.rows_inserted == num_rows | ||
|
|
||
|
|
||
| def test_create_match_filter_single_condition() -> None: | ||
| """ | ||
| Test create_match_filter with a composite key where the source yields exactly one unique key. | ||
|
|
||
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.
This now-passing test fails on
mainwith