|
49 | 49 | import java.nio.ByteBuffer;
|
50 | 50 | import java.util.ArrayList;
|
51 | 51 | import java.util.Collection;
|
| 52 | +import java.util.HashMap; |
52 | 53 | import java.util.List;
|
| 54 | +import java.util.Map; |
53 | 55 | import java.util.UUID;
|
54 | 56 | import java.util.stream.Collectors;
|
55 | 57 |
|
@@ -230,4 +232,38 @@ public static ResolvedPartitionSpec toResolvedPartitionSpec(PbPartitionSpec pbPa
|
230 | 232 | }
|
231 | 233 | return new ResolvedPartitionSpec(partitionKeys, partitionValues);
|
232 | 234 | }
|
| 235 | + |
| 236 | + /** |
| 237 | + * Convert PbPartitionSpec to ResolvedPartitionSpec with partition key ordering. This ensures |
| 238 | + * the partition values are ordered according to the table's partition key sequence. |
| 239 | + */ |
| 240 | + public static ResolvedPartitionSpec toResolvedPartitionSpec( |
| 241 | + PbPartitionSpec pbPartitionSpec, List<String> orderedPartitionKeys) { |
| 242 | + |
| 243 | + List<PbKeyValue> partitionKeyValuesList = pbPartitionSpec.getPartitionKeyValuesList(); |
| 244 | + if (partitionKeyValuesList.size() != orderedPartitionKeys.size()) { |
| 245 | + return toResolvedPartitionSpec(pbPartitionSpec); |
| 246 | + } |
| 247 | + |
| 248 | + Map<String, String> pbkeyValueMap = new HashMap<>(); |
| 249 | + for (PbKeyValue pbKeyValue : partitionKeyValuesList) { |
| 250 | + if (!orderedPartitionKeys.contains(pbKeyValue.getKey())) { |
| 251 | + return toResolvedPartitionSpec(pbPartitionSpec); |
| 252 | + } |
| 253 | + pbkeyValueMap.put(pbKeyValue.getKey(), pbKeyValue.getValue()); |
| 254 | + } |
| 255 | + |
| 256 | + List<String> partitionKeys = new ArrayList<>(); |
| 257 | + List<String> orderedPartitionValues = new ArrayList<>(); |
| 258 | + |
| 259 | + for (String orderedKey : orderedPartitionKeys) { |
| 260 | + String value = pbkeyValueMap.get(orderedKey); |
| 261 | + if (value != null) { |
| 262 | + partitionKeys.add(orderedKey); |
| 263 | + orderedPartitionValues.add(value); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | + return new ResolvedPartitionSpec(partitionKeys, orderedPartitionValues); |
| 268 | + } |
233 | 269 | }
|
0 commit comments