|
| 1 | +# Copyright 2025 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 | + |
| 15 | +import dataclasses |
| 16 | +from typing import cast |
| 17 | + |
| 18 | +from bigframes.core import expression, nodes |
| 19 | + |
| 20 | + |
| 21 | +def defer_selection( |
| 22 | + root: nodes.BigFrameNode, |
| 23 | +) -> nodes.BigFrameNode: |
| 24 | + return nodes.bottom_up(root, pull_up_select) |
| 25 | + |
| 26 | + |
| 27 | +def pull_up_select(node: nodes.BigFrameNode) -> nodes.BigFrameNode: |
| 28 | + if isinstance(node, nodes.LeafNode): |
| 29 | + return node |
| 30 | + if isinstance(node, nodes.JoinNode): |
| 31 | + return pull_up_selects_under_join(node) |
| 32 | + if isinstance(node, nodes.ConcatNode): |
| 33 | + return handle_selects_under_concat(node) |
| 34 | + if isinstance(node, nodes.UnaryNode): |
| 35 | + return pull_up_select_unary(node) |
| 36 | + # shouldn't hit this, but not worth crashing over |
| 37 | + return node |
| 38 | + |
| 39 | + |
| 40 | +def pull_up_select_unary(node: nodes.UnaryNode) -> nodes.BigFrameNode: |
| 41 | + child = node.child |
| 42 | + if not isinstance(child, nodes.SelectionNode): |
| 43 | + return node |
| 44 | + |
| 45 | + # Schema-preserving nodes |
| 46 | + if isinstance( |
| 47 | + node, |
| 48 | + ( |
| 49 | + nodes.ReversedNode, |
| 50 | + nodes.OrderByNode, |
| 51 | + nodes.SliceNode, |
| 52 | + nodes.FilterNode, |
| 53 | + nodes.RandomSampleNode, |
| 54 | + ), |
| 55 | + ): |
| 56 | + pushed_down_node: nodes.BigFrameNode = node.remap_refs( |
| 57 | + {id: ref.id for ref, id in child.input_output_pairs} |
| 58 | + ).replace_child(child.child) |
| 59 | + pulled_up_select = cast( |
| 60 | + nodes.SelectionNode, child.replace_child(pushed_down_node) |
| 61 | + ) |
| 62 | + return pulled_up_select |
| 63 | + elif isinstance(node, (nodes.SelectionNode, nodes.ResultNode, nodes.AggregateNode)): |
| 64 | + return node.remap_refs( |
| 65 | + {id: ref.id for ref, id in child.input_output_pairs} |
| 66 | + ).replace_child(child.child) |
| 67 | + elif isinstance(node, nodes.ExplodeNode): |
| 68 | + pushed_down_node = node.remap_refs( |
| 69 | + {id: ref.id for ref, id in child.input_output_pairs} |
| 70 | + ).replace_child(child.child) |
| 71 | + pulled_up_select = cast( |
| 72 | + nodes.SelectionNode, child.replace_child(pushed_down_node) |
| 73 | + ) |
| 74 | + if node.offsets_col: |
| 75 | + pulled_up_select = dataclasses.replace( |
| 76 | + pulled_up_select, |
| 77 | + input_output_pairs=( |
| 78 | + *pulled_up_select.input_output_pairs, |
| 79 | + nodes.AliasedRef( |
| 80 | + expression.DerefOp(node.offsets_col), node.offsets_col |
| 81 | + ), |
| 82 | + ), |
| 83 | + ) |
| 84 | + return pulled_up_select |
| 85 | + elif isinstance(node, nodes.AdditiveNode): |
| 86 | + pushed_down_node = node.replace_additive_base(child.child).remap_refs( |
| 87 | + {id: ref.id for ref, id in child.input_output_pairs} |
| 88 | + ) |
| 89 | + new_selection = ( |
| 90 | + *child.input_output_pairs, |
| 91 | + *( |
| 92 | + nodes.AliasedRef(expression.DerefOp(col.id), col.id) |
| 93 | + for col in node.added_fields |
| 94 | + ), |
| 95 | + ) |
| 96 | + pulled_up_select = dataclasses.replace( |
| 97 | + child, child=pushed_down_node, input_output_pairs=new_selection |
| 98 | + ) |
| 99 | + return pulled_up_select |
| 100 | + # shouldn't hit this, but not worth crashing over |
| 101 | + return node |
| 102 | + |
| 103 | + |
| 104 | +def pull_up_selects_under_join(node: nodes.JoinNode) -> nodes.JoinNode: |
| 105 | + # Can in theory pull up selects here, but it is a bit dangerous, in particular or self-joins, when there are more transforms to do. |
| 106 | + # TODO: Safely pull up selects above join |
| 107 | + return node |
| 108 | + |
| 109 | + |
| 110 | +def handle_selects_under_concat(node: nodes.ConcatNode) -> nodes.ConcatNode: |
| 111 | + new_children = [] |
| 112 | + for child in node.child_nodes: |
| 113 | + # remove select if no-op |
| 114 | + if not isinstance(child, nodes.SelectionNode): |
| 115 | + new_children.append(child) |
| 116 | + else: |
| 117 | + inputs = (ref.id for ref in child.input_output_pairs) |
| 118 | + if inputs == tuple(child.child.ids): |
| 119 | + new_children.append(child.child) |
| 120 | + else: |
| 121 | + new_children.append(child) |
| 122 | + return dataclasses.replace(node, children=tuple(new_children)) |
0 commit comments