Skip to content

refactor: Reimplement implicit joiner at expression layer #436

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 8 commits into from
Mar 19, 2024

Conversation

TrevorBergeron
Copy link
Contributor

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕

@product-auto-label product-auto-label bot added size: l Pull request size is large. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Mar 13, 2024
@TrevorBergeron TrevorBergeron force-pushed the join_rewrite branch 2 times, most recently from 3d6becb to 5bd7a36 Compare March 13, 2024 22:40
@TrevorBergeron TrevorBergeron marked this pull request as ready for review March 13, 2024 22:46
@TrevorBergeron TrevorBergeron requested review from a team as code owners March 13, 2024 22:46
@TrevorBergeron TrevorBergeron requested review from stevewalker-de and tswast and removed request for stevewalker-de March 13, 2024 22:46
@@ -81,6 +81,12 @@ def output_type(
) -> dtypes.ExpressionType:
...

@abc.abstractmethod
def bind_variables(
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we get a docstring for this, please?

I assume this returns a version of the expression where free variables are replaced with the expressions in the bindings mapping?

What does bind_all control? Looks like it raises a ValueError if there are any UnboundVariables left?

Copy link
Collaborator

Choose a reason for hiding this comment

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

On second look, I see that this is maybe a recursive sort of function where the variables bind themselves? Though maybe the effect is the same where free variables are replaced with expressions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

removed bind_all parameter as it was always set. And yes, this method replaces all the variables in the expression with the corresponding expression given in the bindings input.

class OrderingExpression:
"""A more flexible version of OrderingColumnReference."""

# TODO: Maybe migrate BFET to use this?
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we add a little more detail to this TODO, please?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

extended the todo

@@ -61,6 +63,26 @@ def with_reverse(self):
)


@dataclass(frozen=True)
class OrderingExpression:
"""A more flexible version of OrderingColumnReference."""
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's lead in the first line what the purpose is. e.g. "Defines ordering for an expression" or something like that.

Also, more flexible in what way? That could be a note in the docstring body.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added more info to docstring

direction: OrderingDirection = OrderingDirection.ASC
na_last: bool = True

def remap_names(self, mapping: dict[str, str]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

This needs a docstring. I'm not 100% certain the purpose.

Copy link
Collaborator

Choose a reason for hiding this comment

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

For type annotations, looks like we can use Self here for this builder pattern.

https://www.turingtaco.com/recursive-types/

Copy link
Contributor Author

Choose a reason for hiding this comment

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

wasn't able to get this working. Think we need to be on python 3.11

self.scalar_expression.rename(mapping), self.direction, self.na_last
)

def with_reverse(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Ditto

Suggested change
def with_reverse(self):
def with_reverse(self) -> Self:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I just put the explicit type in new revision as couldn't get Self working. This would be definitely helpful for abstract base classes though.



@dataclasses.dataclass
class NormalizedSelect:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, normalized in what way? Let's do a docstring here too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not really normalized so much as it is the result of squashing together projections, filters and orderbys as much as possible

new_ordering = tuple(expr.with_reverse() for expr in self.ordering)
return NormalizedSelect(self.root, self.columns, self.predicate, new_ordering)

def order_with(self, by: Tuple[order.OrderingColumnReference, ...]):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why not take an OrderingExpression here? Seems a bit odd to add new interfaces that use something that might be deprecated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We are trying to consume and OrderByNode here, and that still uses OrderingColumnReference. I think we might want to migrate these nodes to use the more general OrderingExpression, but that would substantially expand the scope of this PR.

)
if allow_row_identity_join:
as_selection = bigframes.core.rewrite.rewrite_join(join_node)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why doesn't rewrite_join return join_node if it can't rewrite? Seems like this should be called maybe_join like the lower layer does if it might return None.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Comment on lines 102 to 104
if (self.root == right.root) and all(
l_expr == r_expr for l_expr, r_expr in zip(l_join_exprs, r_join_exprs)
):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems we could reduce some nesting if we did the inverse of this and exit early. go/tott/651

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@TrevorBergeron TrevorBergeron requested a review from tswast March 15, 2024 23:43
Copy link
Collaborator

@tswast tswast left a comment

Choose a reason for hiding this comment

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

Thanks!

@tswast tswast added the automerge Merge the pull request once unit tests and other checks pass. label Mar 18, 2024
@gcf-merge-on-green gcf-merge-on-green bot merged commit 6611c28 into main Mar 19, 2024
@gcf-merge-on-green gcf-merge-on-green bot deleted the join_rewrite branch March 19, 2024 01:50
@gcf-merge-on-green gcf-merge-on-green bot removed the automerge Merge the pull request once unit tests and other checks pass. label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: l Pull request size is large.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants