-
Notifications
You must be signed in to change notification settings - Fork 48
perf: Improve repr performance #918
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
Show all changes
9 commits
Select commit
Hold shift + click to select a range
b378198
perf: Improve repr performance
TrevorBergeron 4d0676f
Merge remote-tracking branch 'github/main' into faster_repr
TrevorBergeron 7d74511
extract gbq metadata from nodes to common struct
TrevorBergeron a54d3a6
clarify fast head
TrevorBergeron dd23425
fix physical_schema to be bq client types
TrevorBergeron 1bf4c3b
add classmethod annotation to GbqTable struct factory method
TrevorBergeron 0f351e2
add classmethod annotation to GbqTable struct factory method
TrevorBergeron d173181
Merge branch 'main' into faster_repr
TrevorBergeron 06aa294
Merge branch 'main' into faster_repr
tswast 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
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 |
---|---|---|
|
@@ -312,18 +312,36 @@ def transform_children( | |
|
||
# Input Nodex | ||
@dataclass(frozen=True) | ||
class ReadLocalNode(BigFrameNode): | ||
class LeafNode(BigFrameNode): | ||
@property | ||
def roots(self) -> typing.Set[BigFrameNode]: | ||
return {self} | ||
|
||
@property | ||
def supports_fast_head(self) -> bool: | ||
return False | ||
|
||
def transform_children( | ||
self, t: Callable[[BigFrameNode], BigFrameNode] | ||
) -> BigFrameNode: | ||
return self | ||
|
||
@property | ||
def row_count(self) -> typing.Optional[int]: | ||
"""How many rows are in the data source. None means unknown.""" | ||
return None | ||
|
||
|
||
@dataclass(frozen=True) | ||
class ReadLocalNode(LeafNode): | ||
feather_bytes: bytes | ||
data_schema: schemata.ArraySchema | ||
n_rows: int | ||
session: typing.Optional[bigframes.session.Session] = None | ||
|
||
def __hash__(self): | ||
return self._node_hash | ||
|
||
@property | ||
def roots(self) -> typing.Set[BigFrameNode]: | ||
return {self} | ||
|
||
@functools.cached_property | ||
def schema(self) -> schemata.ArraySchema: | ||
return self.data_schema | ||
|
@@ -333,6 +351,10 @@ def variables_introduced(self) -> int: | |
"""Defines the number of variables generated by the current node. Used to estimate query planning complexity.""" | ||
return len(self.schema.items) + 1 | ||
|
||
@property | ||
def supports_fast_head(self) -> bool: | ||
return True | ||
|
||
@property | ||
def order_ambiguous(self) -> bool: | ||
return False | ||
|
@@ -341,20 +363,38 @@ def order_ambiguous(self) -> bool: | |
def explicitly_ordered(self) -> bool: | ||
return True | ||
|
||
def transform_children( | ||
self, t: Callable[[BigFrameNode], BigFrameNode] | ||
) -> BigFrameNode: | ||
return self | ||
@property | ||
def row_count(self) -> typing.Optional[int]: | ||
return self.n_rows | ||
|
||
|
||
## Put ordering in here or just add order_by node above? | ||
@dataclass(frozen=True) | ||
class ReadTableNode(BigFrameNode): | ||
class GbqTable: | ||
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. This is so we can get something hashable? So we can make sure we only have the fields we care about? A docstring with the purpose would be helpful here. |
||
project_id: str = field() | ||
dataset_id: str = field() | ||
table_id: str = field() | ||
|
||
physical_schema: Tuple[bq.SchemaField, ...] = field() | ||
n_rows: int = field() | ||
cluster_cols: typing.Optional[Tuple[str, ...]] | ||
|
||
@staticmethod | ||
def from_table(table: bq.Table) -> GbqTable: | ||
return GbqTable( | ||
project_id=table.project, | ||
dataset_id=table.dataset_id, | ||
table_id=table.table_id, | ||
physical_schema=tuple(table.schema), | ||
n_rows=table.num_rows, | ||
cluster_cols=None | ||
if table.clustering_fields is None | ||
else tuple(table.clustering_fields), | ||
) | ||
|
||
|
||
## Put ordering in here or just add order_by node above? | ||
@dataclass(frozen=True) | ||
class ReadTableNode(LeafNode): | ||
table: GbqTable | ||
# Subset of physical schema columns, with chosen BQ types | ||
columns: schemata.ArraySchema = field() | ||
|
||
|
@@ -370,10 +410,10 @@ class ReadTableNode(BigFrameNode): | |
|
||
def __post_init__(self): | ||
# enforce invariants | ||
physical_names = set(map(lambda i: i.name, self.physical_schema)) | ||
physical_names = set(map(lambda i: i.name, self.table.physical_schema)) | ||
if not set(self.columns.names).issubset(physical_names): | ||
raise ValueError( | ||
f"Requested schema {self.columns} cannot be derived from table schemal {self.physical_schema}" | ||
f"Requested schema {self.columns} cannot be derived from table schemal {self.table.physical_schema}" | ||
) | ||
if self.order_col_is_sequential and len(self.total_order_cols) != 1: | ||
raise ValueError("Sequential primary key must have only one component") | ||
|
@@ -385,10 +425,6 @@ def session(self): | |
def __hash__(self): | ||
return self._node_hash | ||
|
||
@property | ||
def roots(self) -> typing.Set[BigFrameNode]: | ||
return {self} | ||
|
||
@property | ||
def schema(self) -> schemata.ArraySchema: | ||
return self.columns | ||
|
@@ -398,6 +434,13 @@ def relation_ops_created(self) -> int: | |
# Assume worst case, where readgbq actually has baked in analytic operation to generate index | ||
return 3 | ||
|
||
@property | ||
def supports_fast_head(self) -> bool: | ||
# Fast head is only supported when row offsets are available. | ||
# In the future, ORDER BY+LIMIT optimizations may allow fast head when | ||
# clustered and/or partitioned on ordering key | ||
return self.order_col_is_sequential | ||
|
||
@property | ||
def order_ambiguous(self) -> bool: | ||
return len(self.total_order_cols) == 0 | ||
|
@@ -410,37 +453,34 @@ def explicitly_ordered(self) -> bool: | |
def variables_introduced(self) -> int: | ||
return len(self.schema.items) + 1 | ||
|
||
def transform_children( | ||
self, t: Callable[[BigFrameNode], BigFrameNode] | ||
) -> BigFrameNode: | ||
return self | ||
@property | ||
def row_count(self) -> typing.Optional[int]: | ||
if self.sql_predicate is None: | ||
return self.table.n_rows | ||
return None | ||
|
||
|
||
# This node shouldn't be used in the "original" expression tree, only used as replacement for original during planning | ||
@dataclass(frozen=True) | ||
class CachedTableNode(BigFrameNode): | ||
class CachedTableNode(LeafNode): | ||
# The original BFET subtree that was cached | ||
# note: this isn't a "child" node. | ||
original_node: BigFrameNode = field() | ||
# reference to cached materialization of original_node | ||
project_id: str = field() | ||
dataset_id: str = field() | ||
table_id: str = field() | ||
physical_schema: Tuple[bq.SchemaField, ...] = field() | ||
|
||
table: GbqTable | ||
ordering: typing.Optional[orderings.RowOrdering] = field() | ||
|
||
def __post_init__(self): | ||
# enforce invariants | ||
physical_names = set(map(lambda i: i.name, self.physical_schema)) | ||
physical_names = set(map(lambda i: i.name, self.table.physical_schema)) | ||
logical_names = self.original_node.schema.names | ||
if not set(logical_names).issubset(physical_names): | ||
raise ValueError( | ||
f"Requested schema {logical_names} cannot be derived from table schema {self.physical_schema}" | ||
f"Requested schema {logical_names} cannot be derived from table schema {self.table.physical_schema}" | ||
) | ||
if not set(self.hidden_columns).issubset(physical_names): | ||
raise ValueError( | ||
f"Requested hidden columns {self.hidden_columns} cannot be derived from table schema {self.physical_schema}" | ||
f"Requested hidden columns {self.hidden_columns} cannot be derived from table schema {self.table.physical_schema}" | ||
) | ||
|
||
@property | ||
|
@@ -450,10 +490,6 @@ def session(self): | |
def __hash__(self): | ||
return self._node_hash | ||
|
||
@property | ||
def roots(self) -> typing.Set[BigFrameNode]: | ||
return {self} | ||
|
||
@property | ||
def schema(self) -> schemata.ArraySchema: | ||
return self.original_node.schema | ||
|
@@ -473,6 +509,13 @@ def hidden_columns(self) -> typing.Tuple[str, ...]: | |
if col not in self.schema.names | ||
) | ||
|
||
@property | ||
def supports_fast_head(self) -> bool: | ||
# Fast head is only supported when row offsets are available. | ||
# In the future, ORDER BY+LIMIT optimizations may allow fast head when | ||
# clustered and/or partitioned on ordering key | ||
return (self.ordering is None) or self.ordering.is_sequential | ||
|
||
@property | ||
def order_ambiguous(self) -> bool: | ||
return not isinstance(self.ordering, orderings.TotalOrdering) | ||
|
@@ -483,10 +526,9 @@ def explicitly_ordered(self) -> bool: | |
self.ordering.all_ordering_columns | ||
) > 0 | ||
|
||
def transform_children( | ||
self, t: Callable[[BigFrameNode], BigFrameNode] | ||
) -> BigFrameNode: | ||
return self | ||
@property | ||
def row_count(self) -> typing.Optional[int]: | ||
return self.table.n_rows | ||
|
||
|
||
# Unary nodes | ||
|
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.
I assume this was dead code?
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.
yes, probably from a recent refactor