|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.paimon.operation.commit; |
| 20 | + |
| 21 | +import org.apache.paimon.io.DataFileMeta; |
| 22 | +import org.apache.paimon.schema.SchemaManager; |
| 23 | +import org.apache.paimon.table.SpecialFields; |
| 24 | +import org.apache.paimon.types.DataField; |
| 25 | +import org.apache.paimon.utils.Range; |
| 26 | +import org.apache.paimon.utils.RangeHelper; |
| 27 | + |
| 28 | +import java.util.ArrayList; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Comparator; |
| 31 | +import java.util.HashMap; |
| 32 | +import java.util.HashSet; |
| 33 | +import java.util.List; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.Set; |
| 36 | +import java.util.stream.Collectors; |
| 37 | + |
| 38 | +/** |
| 39 | + * Detects row-id range conflicts only when written field ids overlap. The detection process is as |
| 40 | + * below: |
| 41 | + * |
| 42 | + * <ol> |
| 43 | + * <li>Merge delta files by row range and calculate updated columns. |
| 44 | + * <li>Sort those items by range. |
| 45 | + * <li>For each checking files, do binary search to find overlapping ranges. If their updated |
| 46 | + * columns also overlap, return conflicting result. |
| 47 | + * </ol> |
| 48 | + */ |
| 49 | +public class RowIdColumnConflictChecker { |
| 50 | + |
| 51 | + private final SchemaManager schemaManager; |
| 52 | + private final List<WriteRange> writeRanges; |
| 53 | + private final Map<Long, Map<String, Integer>> fieldIdByNameCache = new HashMap<>(); |
| 54 | + |
| 55 | + private RowIdColumnConflictChecker(SchemaManager schemaManager, List<DataFileMeta> deltaFiles) { |
| 56 | + this.schemaManager = schemaManager; |
| 57 | + this.writeRanges = buildWriteRanges(deltaFiles); |
| 58 | + } |
| 59 | + |
| 60 | + public static RowIdColumnConflictChecker fromDataFiles( |
| 61 | + SchemaManager schemaManager, List<DataFileMeta> deltaFiles) { |
| 62 | + return new RowIdColumnConflictChecker(schemaManager, deltaFiles); |
| 63 | + } |
| 64 | + |
| 65 | + private List<WriteRange> buildWriteRanges(List<DataFileMeta> deltaFiles) { |
| 66 | + List<DataFileMeta> rowIdFiles = |
| 67 | + deltaFiles.stream() |
| 68 | + .filter(file -> file.firstRowId() != null) |
| 69 | + .collect(Collectors.toList()); |
| 70 | + |
| 71 | + if (rowIdFiles.isEmpty()) { |
| 72 | + return Collections.emptyList(); |
| 73 | + } |
| 74 | + |
| 75 | + // 1. merge overlapping ranges and calculate [Range, Set<FieldId>] tuples. |
| 76 | + RangeHelper<DataFileMeta> rangeHelper = new RangeHelper<>(DataFileMeta::nonNullRowIdRange); |
| 77 | + List<WriteRange> writeRanges = new ArrayList<>(); |
| 78 | + for (List<DataFileMeta> group : rangeHelper.mergeOverlappingRanges(rowIdFiles)) { |
| 79 | + Range range = mergeRange(group); |
| 80 | + Set<Integer> fieldIds = new HashSet<>(); |
| 81 | + for (DataFileMeta file : group) { |
| 82 | + addWriteFieldIds(fieldIds, file); |
| 83 | + } |
| 84 | + |
| 85 | + writeRanges.add(new WriteRange(range, fieldIds)); |
| 86 | + } |
| 87 | + |
| 88 | + // 2. sort by range for binary search |
| 89 | + writeRanges.sort( |
| 90 | + Comparator.comparingLong((WriteRange writeRange) -> writeRange.range.from) |
| 91 | + .thenComparingLong(writeRange -> writeRange.range.to)); |
| 92 | + |
| 93 | + return writeRanges; |
| 94 | + } |
| 95 | + |
| 96 | + private void addWriteFieldIds(Set<Integer> fieldIds, DataFileMeta file) { |
| 97 | + List<String> writeCols = file.writeCols(); |
| 98 | + if (writeCols == null) { |
| 99 | + fieldIds.addAll( |
| 100 | + fieldIdByNameCache |
| 101 | + .computeIfAbsent(file.schemaId(), this::fieldIdByName) |
| 102 | + .values()); |
| 103 | + return; |
| 104 | + } |
| 105 | + |
| 106 | + for (String writeCol : writeCols) { |
| 107 | + Integer fieldId = fieldId(file, writeCol); |
| 108 | + if (fieldId != null) { |
| 109 | + fieldIds.add(fieldId); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + private static Range mergeRange(List<DataFileMeta> files) { |
| 115 | + long from = Long.MAX_VALUE; |
| 116 | + long to = Long.MIN_VALUE; |
| 117 | + for (DataFileMeta file : files) { |
| 118 | + Range range = file.nonNullRowIdRange(); |
| 119 | + from = Math.min(from, range.from); |
| 120 | + to = Math.max(to, range.to); |
| 121 | + } |
| 122 | + return new Range(from, to); |
| 123 | + } |
| 124 | + |
| 125 | + boolean isEmpty() { |
| 126 | + return writeRanges.isEmpty(); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Check whether a committed incremental file entry conflicts with current committing delta |
| 131 | + * files. If an existing file has both overlapping row range and overlapping write fields, then |
| 132 | + * it conflicts. |
| 133 | + * |
| 134 | + * @param file committed incremental data file |
| 135 | + * @return true if conflict |
| 136 | + */ |
| 137 | + boolean conflictsWith(DataFileMeta file) { |
| 138 | + Long firstRowId = file.firstRowId(); |
| 139 | + if (firstRowId == null) { |
| 140 | + return false; |
| 141 | + } |
| 142 | + |
| 143 | + Range range = new Range(firstRowId, firstRowId + file.rowCount() - 1); |
| 144 | + int index = firstPossibleRange(range); |
| 145 | + while (index < writeRanges.size()) { |
| 146 | + WriteRange writeRange = writeRanges.get(index); |
| 147 | + if (writeRange.range.from > range.to) { |
| 148 | + return false; |
| 149 | + } |
| 150 | + // overlapping row range and overlapping write fields |
| 151 | + if (writeRange.range.hasIntersection(range) |
| 152 | + && containsAnyWriteField(writeRange.fieldIds, file)) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + index++; |
| 156 | + } |
| 157 | + return false; |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * Binary search to find the first range whose `to` >= target range's `from`. |
| 162 | + * |
| 163 | + * @param range querying range |
| 164 | + * @return index of the first range |
| 165 | + */ |
| 166 | + private int firstPossibleRange(Range range) { |
| 167 | + int low = 0; |
| 168 | + int high = writeRanges.size(); |
| 169 | + while (low < high) { |
| 170 | + int mid = (low + high) >>> 1; |
| 171 | + if (writeRanges.get(mid).range.to < range.from) { |
| 172 | + low = mid + 1; |
| 173 | + } else { |
| 174 | + high = mid; |
| 175 | + } |
| 176 | + } |
| 177 | + return low; |
| 178 | + } |
| 179 | + |
| 180 | + private boolean containsAnyWriteField(Set<Integer> fieldIds, DataFileMeta file) { |
| 181 | + List<String> writeCols = file.writeCols(); |
| 182 | + // If write cols == null, it's a full-schema write |
| 183 | + if (writeCols == null) { |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + for (String writeCol : writeCols) { |
| 188 | + Integer fieldId = fieldId(file, writeCol); |
| 189 | + if (fieldId != null && fieldIds.contains(fieldId)) { |
| 190 | + return true; |
| 191 | + } |
| 192 | + } |
| 193 | + return false; |
| 194 | + } |
| 195 | + |
| 196 | + private Integer fieldId(DataFileMeta file, String writeCol) { |
| 197 | + Integer fieldId = |
| 198 | + fieldIdByNameCache |
| 199 | + .computeIfAbsent(file.schemaId(), this::fieldIdByName) |
| 200 | + .get(writeCol); |
| 201 | + if (fieldId == null) { |
| 202 | + if (SpecialFields.isSystemField(writeCol)) { |
| 203 | + return null; |
| 204 | + } |
| 205 | + throw new RuntimeException( |
| 206 | + String.format( |
| 207 | + "Cannot find write column '%s' in schema %s.", |
| 208 | + writeCol, file.schemaId())); |
| 209 | + } |
| 210 | + return fieldId; |
| 211 | + } |
| 212 | + |
| 213 | + private Map<String, Integer> fieldIdByName(long schemaId) { |
| 214 | + Map<String, Integer> fieldIdByName = new HashMap<>(); |
| 215 | + for (DataField field : schemaManager.schema(schemaId).logicalRowType().getFields()) { |
| 216 | + fieldIdByName.put(field.name(), field.id()); |
| 217 | + } |
| 218 | + return fieldIdByName; |
| 219 | + } |
| 220 | + |
| 221 | + /** Range and field id Set. */ |
| 222 | + private static class WriteRange { |
| 223 | + |
| 224 | + private final Range range; |
| 225 | + private final Set<Integer> fieldIds; |
| 226 | + |
| 227 | + private WriteRange(Range range, Set<Integer> fieldIds) { |
| 228 | + this.range = range; |
| 229 | + this.fieldIds = fieldIds; |
| 230 | + } |
| 231 | + } |
| 232 | +} |
0 commit comments