|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +import functools |
| 18 | +import operator |
| 19 | + |
| 20 | +import pyarrow as pa |
| 21 | +from pyarrow import Table as pyarrow_table |
| 22 | +from pyarrow import compute as pc |
| 23 | + |
| 24 | +from pyiceberg.expressions import ( |
| 25 | + And, |
| 26 | + BooleanExpression, |
| 27 | + EqualTo, |
| 28 | + In, |
| 29 | + Or, |
| 30 | +) |
| 31 | + |
| 32 | + |
| 33 | +def create_match_filter(df: pyarrow_table, join_cols: list[str]) -> BooleanExpression: |
| 34 | + unique_keys = df.select(join_cols).group_by(join_cols).aggregate([]) |
| 35 | + |
| 36 | + if len(join_cols) == 1: |
| 37 | + return In(join_cols[0], unique_keys[0].to_pylist()) |
| 38 | + else: |
| 39 | + return Or(*[And(*[EqualTo(col, row[col]) for col in join_cols]) for row in unique_keys.to_pylist()]) |
| 40 | + |
| 41 | + |
| 42 | +def has_duplicate_rows(df: pyarrow_table, join_cols: list[str]) -> bool: |
| 43 | + """Check for duplicate rows in a PyArrow table based on the join columns.""" |
| 44 | + return len(df.select(join_cols).group_by(join_cols).aggregate([([], "count_all")]).filter(pc.field("count_all") > 1)) > 0 |
| 45 | + |
| 46 | + |
| 47 | +def get_rows_to_update(source_table: pa.Table, target_table: pa.Table, join_cols: list[str]) -> pa.Table: |
| 48 | + """ |
| 49 | + Return a table with rows that need to be updated in the target table based on the join columns. |
| 50 | +
|
| 51 | + When a row is matched, an additional scan is done to evaluate the non-key columns to detect if an actual change has occurred. |
| 52 | + Only matched rows that have an actual change to a non-key column value will be returned in the final output. |
| 53 | + """ |
| 54 | + all_columns = set(source_table.column_names) |
| 55 | + join_cols_set = set(join_cols) |
| 56 | + |
| 57 | + non_key_cols = list(all_columns - join_cols_set) |
| 58 | + |
| 59 | + match_expr = functools.reduce(operator.and_, [pc.field(col).isin(target_table.column(col).to_pylist()) for col in join_cols]) |
| 60 | + |
| 61 | + matching_source_rows = source_table.filter(match_expr) |
| 62 | + |
| 63 | + rows_to_update = [] |
| 64 | + |
| 65 | + for index in range(matching_source_rows.num_rows): |
| 66 | + source_row = matching_source_rows.slice(index, 1) |
| 67 | + |
| 68 | + target_filter = functools.reduce(operator.and_, [pc.field(col) == source_row.column(col)[0].as_py() for col in join_cols]) |
| 69 | + |
| 70 | + matching_target_row = target_table.filter(target_filter) |
| 71 | + |
| 72 | + if matching_target_row.num_rows > 0: |
| 73 | + needs_update = False |
| 74 | + |
| 75 | + for non_key_col in non_key_cols: |
| 76 | + source_value = source_row.column(non_key_col)[0].as_py() |
| 77 | + target_value = matching_target_row.column(non_key_col)[0].as_py() |
| 78 | + |
| 79 | + if source_value != target_value: |
| 80 | + needs_update = True |
| 81 | + break |
| 82 | + |
| 83 | + if needs_update: |
| 84 | + rows_to_update.append(source_row) |
| 85 | + |
| 86 | + if rows_to_update: |
| 87 | + rows_to_update_table = pa.concat_tables(rows_to_update) |
| 88 | + else: |
| 89 | + rows_to_update_table = pa.Table.from_arrays([], names=source_table.column_names) |
| 90 | + |
| 91 | + common_columns = set(source_table.column_names).intersection(set(target_table.column_names)) |
| 92 | + rows_to_update_table = rows_to_update_table.select(list(common_columns)) |
| 93 | + |
| 94 | + return rows_to_update_table |
| 95 | + |
| 96 | + |
| 97 | +def get_rows_to_insert(source_table: pa.Table, target_table: pa.Table, join_cols: list[str]) -> pa.Table: |
| 98 | + source_filter_expr = pc.scalar(True) |
| 99 | + |
| 100 | + for col in join_cols: |
| 101 | + target_values = target_table.column(col).to_pylist() |
| 102 | + expr = pc.field(col).isin(target_values) |
| 103 | + |
| 104 | + if source_filter_expr is None: |
| 105 | + source_filter_expr = expr |
| 106 | + else: |
| 107 | + source_filter_expr = source_filter_expr & expr |
| 108 | + |
| 109 | + non_matching_expr = ~source_filter_expr |
| 110 | + |
| 111 | + source_columns = set(source_table.column_names) |
| 112 | + target_columns = set(target_table.column_names) |
| 113 | + |
| 114 | + common_columns = source_columns.intersection(target_columns) |
| 115 | + |
| 116 | + non_matching_rows = source_table.filter(non_matching_expr).select(common_columns) |
| 117 | + |
| 118 | + return non_matching_rows |
0 commit comments