|
| 1 | +# Copyright 2023 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import typing |
| 17 | + |
| 18 | +import bigframes_vendored.ibis |
| 19 | + |
| 20 | +import bigframes.core.compile.compiled as compiled |
| 21 | +import bigframes.core.expression as ex |
| 22 | +import bigframes.core.guid |
| 23 | +import bigframes.core.identifiers as ids |
| 24 | +import bigframes.core.ordering |
| 25 | +from bigframes.core.ordering import TotalOrdering |
| 26 | + |
| 27 | + |
| 28 | +def explode_unordered( |
| 29 | + input: compiled.UnorderedIR, |
| 30 | + columns: typing.Sequence[ex.DerefOp], |
| 31 | + offsets_id: typing.Optional[str], |
| 32 | +) -> compiled.UnorderedIR: |
| 33 | + table = input._to_ibis_expr() |
| 34 | + column_ids = tuple(ref.id.sql for ref in columns) |
| 35 | + |
| 36 | + # The offset array ensures null represents empty arrays after unnesting. |
| 37 | + offset_array_id = bigframes.core.guid.generate_guid("offset_array_") |
| 38 | + offset_array = bigframes_vendored.ibis.range( |
| 39 | + 0, |
| 40 | + bigframes_vendored.ibis.greatest( |
| 41 | + 1, # We always want at least 1 element to fill in NULLs for empty arrays. |
| 42 | + bigframes_vendored.ibis.least( |
| 43 | + *[table[column_id].length() for column_id in column_ids] |
| 44 | + ), |
| 45 | + ), |
| 46 | + 1, |
| 47 | + ).name(offset_array_id) |
| 48 | + table_w_offset_array = table.select( |
| 49 | + offset_array, |
| 50 | + *input._column_names, |
| 51 | + ) |
| 52 | + |
| 53 | + unnest_offset_id = offsets_id or bigframes.core.guid.generate_guid("unnest_offset_") |
| 54 | + unnest_offset = ( |
| 55 | + table_w_offset_array[offset_array_id].unnest().name(unnest_offset_id) |
| 56 | + ) |
| 57 | + table_w_offset = table_w_offset_array.select( |
| 58 | + unnest_offset, |
| 59 | + *input._column_names, |
| 60 | + ) |
| 61 | + |
| 62 | + output_cols = tuple(input.column_ids) + ((offsets_id,) if offsets_id else ()) |
| 63 | + unnested_columns = [ |
| 64 | + table_w_offset[column_id][table_w_offset[unnest_offset_id]].name(column_id) |
| 65 | + if column_id in column_ids |
| 66 | + else table_w_offset[column_id] |
| 67 | + for column_id in output_cols |
| 68 | + ] |
| 69 | + table_w_unnest = table_w_offset.select(*unnested_columns) |
| 70 | + |
| 71 | + columns = [table_w_unnest[column_name] for column_name in output_cols] |
| 72 | + return compiled.UnorderedIR( |
| 73 | + table_w_unnest, |
| 74 | + columns=columns, # type: ignore |
| 75 | + ) |
| 76 | + |
| 77 | + |
| 78 | +def explode_ordered( |
| 79 | + input: compiled.OrderedIR, |
| 80 | + columns: typing.Sequence[ex.DerefOp], |
| 81 | + offsets_id: typing.Optional[str], |
| 82 | +) -> compiled.OrderedIR: |
| 83 | + if input.order_non_deterministic: |
| 84 | + id = bigframes.core.guid.generate_guid() |
| 85 | + return input.promote_offsets(id) |
| 86 | + table = input._to_ibis_expr(ordering_mode="unordered", expose_hidden_cols=True) |
| 87 | + column_ids = tuple(ref.id.sql for ref in columns) |
| 88 | + |
| 89 | + offset_array_id = bigframes.core.guid.generate_guid("offset_array_") |
| 90 | + offset_array = bigframes_vendored.ibis.range( |
| 91 | + 0, |
| 92 | + bigframes_vendored.ibis.greatest( |
| 93 | + 1, # We always want at least 1 element to fill in NULLs for empty arrays. |
| 94 | + bigframes_vendored.ibis.least( |
| 95 | + *[table[column_id].length() for column_id in column_ids] |
| 96 | + ), |
| 97 | + ), |
| 98 | + 1, |
| 99 | + ).name(offset_array_id) |
| 100 | + table_w_offset_array = table.select( |
| 101 | + offset_array, |
| 102 | + *input._column_names, |
| 103 | + *input._hidden_ordering_column_names, |
| 104 | + ) |
| 105 | + |
| 106 | + unnest_offset_id = offsets_id or bigframes.core.guid.generate_guid("unnest_offset_") |
| 107 | + unnest_offset = ( |
| 108 | + table_w_offset_array[offset_array_id].unnest().name(unnest_offset_id) |
| 109 | + ) |
| 110 | + table_w_offset = table_w_offset_array.select( |
| 111 | + unnest_offset, |
| 112 | + *input._column_names, |
| 113 | + *input._hidden_ordering_column_names, |
| 114 | + ) |
| 115 | + |
| 116 | + unnested_columns = [ |
| 117 | + table_w_offset[column_id][table_w_offset[unnest_offset_id]].name(column_id) |
| 118 | + if column_id in column_ids |
| 119 | + else table_w_offset[column_id] |
| 120 | + for column_id in input._column_names |
| 121 | + ] |
| 122 | + |
| 123 | + table_w_unnest = table_w_offset.select( |
| 124 | + table_w_offset[unnest_offset_id], |
| 125 | + *unnested_columns, |
| 126 | + *input._hidden_ordering_column_names, |
| 127 | + ) |
| 128 | + |
| 129 | + output_cols = tuple(input.column_ids) + ((offsets_id,) if offsets_id else ()) |
| 130 | + columns = [table_w_unnest[column_name] for column_name in output_cols] |
| 131 | + hidden_ordering_columns = [ |
| 132 | + table_w_unnest[column_name] |
| 133 | + for column_name in input._hidden_ordering_column_names |
| 134 | + ] |
| 135 | + if offsets_id is None: |
| 136 | + hidden_ordering_columns.append(table_w_unnest[unnest_offset_id]) |
| 137 | + l_mappings = {id: id for id in input._ordering.referenced_columns} |
| 138 | + r_mappings = {ids.ColumnId(unnest_offset_id): ids.ColumnId(unnest_offset_id)} |
| 139 | + ordering = bigframes.core.ordering.join_orderings( |
| 140 | + input._ordering, |
| 141 | + TotalOrdering.from_offset_col(unnest_offset_id), |
| 142 | + l_mappings, |
| 143 | + r_mappings, |
| 144 | + ) |
| 145 | + |
| 146 | + return compiled.OrderedIR( |
| 147 | + table_w_unnest, |
| 148 | + columns=columns, # type: ignore |
| 149 | + hidden_ordering_columns=hidden_ordering_columns, |
| 150 | + ordering=ordering, |
| 151 | + ) |
0 commit comments