|
| 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 bigframes.core as core |
| 16 | +import bigframes.core.identifiers as identifiers |
| 17 | +import bigframes.core.nodes as nodes |
| 18 | +import bigframes.core.rewrite.identifiers as id_rewrite |
| 19 | + |
| 20 | + |
| 21 | +def test_remap_variables_single_node(leaf): |
| 22 | + node = leaf |
| 23 | + id_generator = (identifiers.ColumnId(f"id_{i}") for i in range(100)) |
| 24 | + new_node, mapping = id_rewrite.remap_variables(node, id_generator) |
| 25 | + assert new_node is not node |
| 26 | + assert len(mapping) == 2 |
| 27 | + assert set(mapping.keys()) == {f.id for f in node.fields} |
| 28 | + assert set(mapping.values()) == { |
| 29 | + identifiers.ColumnId("id_0"), |
| 30 | + identifiers.ColumnId("id_1"), |
| 31 | + } |
| 32 | + |
| 33 | + |
| 34 | +def test_remap_variables_projection(leaf): |
| 35 | + node = nodes.ProjectionNode( |
| 36 | + leaf, |
| 37 | + ( |
| 38 | + ( |
| 39 | + core.expression.DerefOp(leaf.fields[0].id), |
| 40 | + identifiers.ColumnId("new_col"), |
| 41 | + ), |
| 42 | + ), |
| 43 | + ) |
| 44 | + id_generator = (identifiers.ColumnId(f"id_{i}") for i in range(100)) |
| 45 | + new_node, mapping = id_rewrite.remap_variables(node, id_generator) |
| 46 | + assert new_node is not node |
| 47 | + assert len(mapping) == 3 |
| 48 | + assert set(mapping.keys()) == {f.id for f in node.fields} |
| 49 | + assert set(mapping.values()) == {identifiers.ColumnId(f"id_{i}") for i in range(3)} |
| 50 | + |
| 51 | + |
| 52 | +def test_remap_variables_nested_join_stability(leaf, fake_session, table): |
| 53 | + # Create two more distinct leaf nodes |
| 54 | + leaf2_uncached = core.ArrayValue.from_table( |
| 55 | + session=fake_session, |
| 56 | + table=table, |
| 57 | + schema=leaf.schema, |
| 58 | + ).node |
| 59 | + leaf2 = leaf2_uncached.remap_vars( |
| 60 | + { |
| 61 | + field.id: identifiers.ColumnId(f"leaf2_{field.id.name}") |
| 62 | + for field in leaf2_uncached.fields |
| 63 | + } |
| 64 | + ) |
| 65 | + leaf3_uncached = core.ArrayValue.from_table( |
| 66 | + session=fake_session, |
| 67 | + table=table, |
| 68 | + schema=leaf.schema, |
| 69 | + ).node |
| 70 | + leaf3 = leaf3_uncached.remap_vars( |
| 71 | + { |
| 72 | + field.id: identifiers.ColumnId(f"leaf3_{field.id.name}") |
| 73 | + for field in leaf3_uncached.fields |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + # Create a nested join: (leaf JOIN leaf2) JOIN leaf3 |
| 78 | + inner_join = nodes.JoinNode( |
| 79 | + left_child=leaf, |
| 80 | + right_child=leaf2, |
| 81 | + conditions=( |
| 82 | + ( |
| 83 | + core.expression.DerefOp(leaf.fields[0].id), |
| 84 | + core.expression.DerefOp(leaf2.fields[0].id), |
| 85 | + ), |
| 86 | + ), |
| 87 | + type="inner", |
| 88 | + propogate_order=False, |
| 89 | + ) |
| 90 | + outer_join = nodes.JoinNode( |
| 91 | + left_child=inner_join, |
| 92 | + right_child=leaf3, |
| 93 | + conditions=( |
| 94 | + ( |
| 95 | + core.expression.DerefOp(inner_join.fields[0].id), |
| 96 | + core.expression.DerefOp(leaf3.fields[0].id), |
| 97 | + ), |
| 98 | + ), |
| 99 | + type="inner", |
| 100 | + propogate_order=False, |
| 101 | + ) |
| 102 | + |
| 103 | + # Run remap_variables twice and assert stability |
| 104 | + id_generator1 = (identifiers.ColumnId(f"id_{i}") for i in range(100)) |
| 105 | + new_node1, mapping1 = id_rewrite.remap_variables(outer_join, id_generator1) |
| 106 | + |
| 107 | + id_generator2 = (identifiers.ColumnId(f"id_{i}") for i in range(100)) |
| 108 | + new_node2, mapping2 = id_rewrite.remap_variables(outer_join, id_generator2) |
| 109 | + |
| 110 | + assert new_node1 == new_node2 |
| 111 | + assert mapping1 == mapping2 |
| 112 | + |
| 113 | + |
| 114 | +def test_remap_variables_concat_self_stability(leaf): |
| 115 | + # Create a concat node with the same child twice |
| 116 | + node = nodes.ConcatNode( |
| 117 | + children=(leaf, leaf), |
| 118 | + output_ids=( |
| 119 | + identifiers.ColumnId("concat_a"), |
| 120 | + identifiers.ColumnId("concat_b"), |
| 121 | + ), |
| 122 | + ) |
| 123 | + |
| 124 | + # Run remap_variables twice and assert stability |
| 125 | + id_generator1 = (identifiers.ColumnId(f"id_{i}") for i in range(100)) |
| 126 | + new_node1, mapping1 = id_rewrite.remap_variables(node, id_generator1) |
| 127 | + |
| 128 | + id_generator2 = (identifiers.ColumnId(f"id_{i}") for i in range(100)) |
| 129 | + new_node2, mapping2 = id_rewrite.remap_variables(node, id_generator2) |
| 130 | + |
| 131 | + assert new_node1 == new_node2 |
| 132 | + assert mapping1 == mapping2 |
0 commit comments