GeezerVison is showing github.com — perf: Avoid requery for some result downsample methods (#2219) · google/bigframes@95763ff
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bigframes/core/blocks.py +23-42Lines changed: 23 additions & 42 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -818,49 +818,30 @@ def _materialize_local( | 818 | 818 | total_rows = result_batches.approx_total_rows | 819 | 819 | # Remove downsampling config from subsequent invocations, as otherwise could result in many | 820 | 820 | # iterations if downsampling undershoots | 821 | | - return self._downsample( | 822 | | - total_rows=total_rows, | 823 | | - sampling_method=sample_config.sampling_method, | 824 | | - fraction=fraction, | 825 | | - random_state=sample_config.random_state, | 826 | | - )._materialize_local( | 827 | | - MaterializationOptions(ordered=materialize_options.ordered) | 828 | | - ) | 829 | | - else: | 830 | | - df = result_batches.to_pandas() | 831 | | - df = self._copy_index_to_pandas(df) | 832 | | - df.set_axis(self.column_labels, axis=1, copy=False) | 833 | | - return df, execute_result.query_job | 834 | | - | 835 | | - def _downsample( | 836 | | - self, total_rows: int, sampling_method: str, fraction: float, random_state | 837 | | - ) -> Block: | 838 | | - # either selecting fraction or number of rows | 839 | | - if sampling_method == _HEAD: | 840 | | - filtered_block = self.slice(stop=int(total_rows * fraction)) | 841 | | - return filtered_block | 842 | | - elif (sampling_method == _UNIFORM) and (random_state is None): | 843 | | - filtered_expr = self.expr._uniform_sampling(fraction) | 844 | | - block = Block( | 845 | | - filtered_expr, | 846 | | - index_columns=self.index_columns, | 847 | | - column_labels=self.column_labels, | 848 | | - index_labels=self.index.names, | 849 | | - ) | 850 | | - return block | 851 | | - elif sampling_method == _UNIFORM: | 852 | | - block = self.split( | 853 | | - fracs=(fraction,), | 854 | | - random_state=random_state, | 855 | | - sort=False, | 856 | | - )[0] | 857 | | - return block | | 821 | + if sample_config.sampling_method == "head": | | 822 | + # Just truncates the result iterator without a follow-up query | | 823 | + raw_df = result_batches.to_pandas(limit=int(total_rows * fraction)) | | 824 | + elif ( | | 825 | + sample_config.sampling_method == "uniform" | | 826 | + and sample_config.random_state is None | | 827 | + ): | | 828 | + # Pushes sample into result without new query | | 829 | + sampled_batches = execute_result.batches(sample_rate=fraction) | | 830 | + raw_df = sampled_batches.to_pandas() | | 831 | + else: # uniform sample with random state requires a full follow-up query | | 832 | + down_sampled_block = self.split( | | 833 | + fracs=(fraction,), | | 834 | + random_state=sample_config.random_state, | | 835 | + sort=False, | | 836 | + )[0] | | 837 | + return down_sampled_block._materialize_local( | | 838 | + MaterializationOptions(ordered=materialize_options.ordered) | | 839 | + ) | 858 | 840 | else: | 859 | | - # This part should never be called, just in case. | 860 | | - raise NotImplementedError( | 861 | | - f"The downsampling method {sampling_method} is not implemented, " | 862 | | - f"please choose from {','.join(_SAMPLING_METHODS)}." | 863 | | - ) | | 841 | + raw_df = result_batches.to_pandas() | | 842 | + df = self._copy_index_to_pandas(raw_df) | | 843 | + df.set_axis(self.column_labels, axis=1, copy=False) | | 844 | + return df, execute_result.query_job | 864 | 845 |
| 865 | 846 | def split( | 866 | 847 | self, | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/bq_data.py +12-1Lines changed: 12 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -186,11 +186,22 @@ def get_arrow_batches( | 186 | 186 | columns: Sequence[str], | 187 | 187 | storage_read_client: bigquery_storage_v1.BigQueryReadClient, | 188 | 188 | project_id: str, | | 189 | + sample_rate: Optional[float] = None, | 189 | 190 | ) -> ReadResult: | 190 | 191 | table_mod_options = {} | 191 | 192 | read_options_dict: dict[str, Any] = {"selected_fields": list(columns)} | | 193 | + | | 194 | + predicates = [] | 192 | 195 | if data.sql_predicate: | 193 | | - read_options_dict["row_restriction"] = data.sql_predicate | | 196 | + predicates.append(data.sql_predicate) | | 197 | + if sample_rate is not None: | | 198 | + assert isinstance(sample_rate, float) | | 199 | + predicates.append(f"RAND() < {sample_rate}") | | 200 | + | | 201 | + if predicates: | | 202 | + full_predicates = " AND ".join(f"( {pred} )" for pred in predicates) | | 203 | + read_options_dict["row_restriction"] = full_predicates | | 204 | + | 194 | 205 | read_options = bq_storage_types.ReadSession.TableReadOptions(**read_options_dict) | 195 | 206 |
| 196 | 207 | if data.at_time: | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/core/local_data.py +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 25 | 25 | import uuid | 26 | 26 |
| 27 | 27 | import geopandas # type: ignore | | 28 | +import numpy | 28 | 29 | import numpy as np | 29 | 30 | import pandas as pd | 30 | 31 | import pyarrow as pa | @@ -124,13 +125,21 @@ def to_arrow( | 124 | 125 | geo_format: Literal["wkb", "wkt"] = "wkt", | 125 | 126 | duration_type: Literal["int", "duration"] = "duration", | 126 | 127 | json_type: Literal["string"] = "string", | | 128 | + sample_rate: Optional[float] = None, | 127 | 129 | max_chunksize: Optional[int] = None, | 128 | 130 | ) -> tuple[pa.Schema, Iterable[pa.RecordBatch]]: | 129 | 131 | if geo_format != "wkt": | 130 | 132 | raise NotImplementedError(f"geo format {geo_format} not yet implemented") | 131 | 133 | assert json_type == "string" | 132 | 134 |
| 133 | | - batches = self.data.to_batches(max_chunksize=max_chunksize) | | 135 | + data = self.data | | 136 | + | | 137 | + # This exists for symmetry with remote sources, but sampling local data like this shouldn't really happen | | 138 | + if sample_rate is not None: | | 139 | + to_take = numpy.random.rand(data.num_rows) < sample_rate | | 140 | + data = data.filter(to_take) | | 141 | + | | 142 | + batches = data.to_batches(max_chunksize=max_chunksize) | 134 | 143 | schema = self.data.schema | 135 | 144 | if duration_type == "int": | 136 | 145 | schema = _schema_durations_to_ints(schema) | |
Collapse file Copy file name to clipboardExpand all lines: bigframes/session/executor.py +15-11Lines changed: 15 additions & 11 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -88,7 +88,7 @@ def arrow_batches(self) -> Iterator[pyarrow.RecordBatch]: | 88 | 88 |
| 89 | 89 | yield batch | 90 | 90 |
| 91 | | - def to_arrow_table(self) -> pyarrow.Table: | | 91 | + def to_arrow_table(self, limit: Optional[int] = None) -> pyarrow.Table: | 92 | 92 | # Need to provide schema if no result rows, as arrow can't infer | 93 | 93 | # If ther are rows, it is safest to infer schema from batches. | 94 | 94 | # Any discrepencies between predicted schema and actual schema will produce errors. | @@ -97,18 +97,21 @@ def to_arrow_table(self) -> pyarrow.Table: | 97 | 97 | peek_value = list(peek_it) | 98 | 98 | # TODO: Enforce our internal schema on the table for consistency | 99 | 99 | if len(peek_value) > 0: | 100 | | - return pyarrow.Table.from_batches( | 101 | | - itertools.chain(peek_value, batches), # reconstruct | 102 | | - ) | | 100 | + batches = itertools.chain(peek_value, batches) # reconstruct | | 101 | + if limit: | | 102 | + batches = pyarrow_utils.truncate_pyarrow_iterable( | | 103 | + batches, max_results=limit | | 104 | + ) | | 105 | + return pyarrow.Table.from_batches(batches) | 103 | 106 | else: | 104 | 107 | try: | 105 | 108 | return self._schema.to_pyarrow().empty_table() | 106 | 109 | except pa.ArrowNotImplementedError: | 107 | 110 | # Bug with some pyarrow versions, empty_table only supports base storage types, not extension types. | 108 | 111 | return self._schema.to_pyarrow(use_storage_types=True).empty_table() | 109 | 112 |
| 110 | | - def to_pandas(self) -> pd.DataFrame: | 111 | | - return io_pandas.arrow_to_pandas(self.to_arrow_table(), self._schema) | | 113 | + def to_pandas(self, limit: Optional[int] = None) -> pd.DataFrame: | | 114 | + return io_pandas.arrow_to_pandas(self.to_arrow_table(limit=limit), self._schema) | 112 | 115 |
| 113 | 116 | def to_pandas_batches( | 114 | 117 | self, page_size: Optional[int] = None, max_results: Optional[int] = None | @@ -158,7 +161,7 @@ def schema(self) -> bigframes.core.schema.ArraySchema: | 158 | 161 | ... | 159 | 162 |
| 160 | 163 | @abc.abstractmethod | 161 | | - def batches(self) -> ResultsIterator: | | 164 | + def batches(self, sample_rate: Optional[float] = None) -> ResultsIterator: | 162 | 165 | ... | 163 | 166 |
| 164 | 167 | @property | @@ -200,9 +203,9 @@ def execution_metadata(self) -> ExecutionMetadata: | 200 | 203 | def schema(self) -> bigframes.core.schema.ArraySchema: | 201 | 204 | return self._data.schema | 202 | 205 |
| 203 | | - def batches(self) -> ResultsIterator: | | 206 | + def batches(self, sample_rate: Optional[float] = None) -> ResultsIterator: | 204 | 207 | return ResultsIterator( | 205 | | - iter(self._data.to_arrow()[1]), | | 208 | + iter(self._data.to_arrow(sample_rate=sample_rate)[1]), | 206 | 209 | self.schema, | 207 | 210 | self._data.metadata.row_count, | 208 | 211 | self._data.metadata.total_bytes, | @@ -226,7 +229,7 @@ def execution_metadata(self) -> ExecutionMetadata: | 226 | 229 | def schema(self) -> bigframes.core.schema.ArraySchema: | 227 | 230 | return self._schema | 228 | 231 |
| 229 | | - def batches(self) -> ResultsIterator: | | 232 | + def batches(self, sample_rate: Optional[float] = None) -> ResultsIterator: | 230 | 233 | return ResultsIterator(iter([]), self.schema, 0, 0) | 231 | 234 |
| 232 | 235 |
| @@ -260,12 +263,13 @@ def schema(self) -> bigframes.core.schema.ArraySchema: | 260 | 263 | source_ids = [selection[0] for selection in self._selected_fields] | 261 | 264 | return self._data.schema.select(source_ids).rename(dict(self._selected_fields)) | 262 | 265 |
| 263 | | - def batches(self) -> ResultsIterator: | | 266 | + def batches(self, sample_rate: Optional[float] = None) -> ResultsIterator: | 264 | 267 | read_batches = bq_data.get_arrow_batches( | 265 | 268 | self._data, | 266 | 269 | [x[0] for x in self._selected_fields], | 267 | 270 | self._storage_client, | 268 | 271 | self._project_id, | | 272 | + sample_rate=sample_rate, | 269 | 273 | ) | 270 | 274 | arrow_batches: Iterator[pa.RecordBatch] = map( | 271 | 275 | functools.partial( | |
Collapse file Copy file name to clipboardExpand all lines: tests/system/small/test_anywidget.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -165,7 +165,7 @@ def execution_metadata(self) -> ExecutionMetadata: | 165 | 165 | def schema(self) -> Any: | 166 | 166 | return schema | 167 | 167 |
| 168 | | - def batches(self) -> ResultsIterator: | | 168 | + def batches(self, sample_rate=None) -> ResultsIterator: | 169 | 169 | return ResultsIterator( | 170 | 170 | arrow_batches_val, | 171 | 171 | self.schema, | |
Collapse file Copy file name to clipboardExpand all lines: tests/system/small/test_dataframe.py +3-3Lines changed: 3 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4524,7 +4524,7 @@ def test_df_kurt(scalars_dfs): | 4524 | 4524 | "n_default", | 4525 | 4525 | ], | 4526 | 4526 | ) | 4527 | | -def test_sample(scalars_dfs, frac, n, random_state): | | 4527 | +def test_df_to_pandas_sample(scalars_dfs, frac, n, random_state): | 4528 | 4528 | scalars_df, _ = scalars_dfs | 4529 | 4529 | df = scalars_df.sample(frac=frac, n=n, random_state=random_state) | 4530 | 4530 | bf_result = df.to_pandas() | @@ -4535,15 +4535,15 @@ def test_sample(scalars_dfs, frac, n, random_state): | 4535 | 4535 | assert bf_result.shape[1] == scalars_df.shape[1] | 4536 | 4536 |
| 4537 | 4537 |
| 4538 | | -def test_sample_determinism(penguins_df_default_index): | | 4538 | +def test_df_to_pandas_sample_determinism(penguins_df_default_index): | 4539 | 4539 | df = penguins_df_default_index.sample(n=100, random_state=12345).head(15) | 4540 | 4540 | bf_result = df.to_pandas() | 4541 | 4541 | bf_result2 = df.to_pandas() | 4542 | 4542 |
| 4543 | 4543 | pandas.testing.assert_frame_equal(bf_result, bf_result2) | 4544 | 4544 |
| 4545 | 4545 |
| 4546 | | -def test_sample_raises_value_error(scalars_dfs): | | 4546 | +def test_df_to_pandas_sample_raises_value_error(scalars_dfs): | 4547 | 4547 | scalars_df, _ = scalars_dfs | 4548 | 4548 | with pytest.raises( | 4549 | 4549 | ValueError, match="Only one of 'n' or 'frac' parameter can be specified." | |
|