|
| 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 | +package org.apache.beam.sdk.io.iceberg; |
| 19 | + |
| 20 | +import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; |
| 21 | + |
| 22 | +import java.util.List; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.function.BiFunction; |
| 25 | +import java.util.regex.Matcher; |
| 26 | +import java.util.regex.Pattern; |
| 27 | +import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap; |
| 28 | +import org.apache.iceberg.PartitionSpec; |
| 29 | +import org.apache.iceberg.Schema; |
| 30 | + |
| 31 | +class PartitionUtils { |
| 32 | + private static final Pattern HOUR = Pattern.compile("^hour\\(([a-zA-Z0-9_-]+)\\)$"); |
| 33 | + private static final Pattern DAY = Pattern.compile("^day\\(([a-zA-Z0-9_-]+)\\)$"); |
| 34 | + private static final Pattern MONTH = Pattern.compile("^month\\(([a-zA-Z0-9_-]+)\\)$"); |
| 35 | + private static final Pattern YEAR = Pattern.compile("^year\\(([a-zA-Z0-9_-]+)\\)$"); |
| 36 | + private static final Pattern TRUNCATE = |
| 37 | + Pattern.compile("^truncate\\(([a-zA-Z0-9_-]+),\\s*(\\d+)\\)$"); |
| 38 | + private static final Pattern BUCKET = |
| 39 | + Pattern.compile("^bucket\\(([a-zA-Z0-9_-]+),\\s*(\\d+)\\)$"); |
| 40 | + private static final Pattern VOID = Pattern.compile("^void\\(([^)]+)\\)$"); |
| 41 | + private static final Pattern IDENTITY = Pattern.compile("^([a-zA-Z0-9_-]+)$"); |
| 42 | + |
| 43 | + private static final Map< |
| 44 | + Pattern, BiFunction<PartitionSpec.Builder, Matcher, PartitionSpec.Builder>> |
| 45 | + TRANSFORMATIONS = |
| 46 | + ImmutableMap.of( |
| 47 | + HOUR, (builder, matcher) -> builder.hour(checkStateNotNull(matcher.group(1))), |
| 48 | + DAY, (builder, matcher) -> builder.day(checkStateNotNull(matcher.group(1))), |
| 49 | + MONTH, (builder, matcher) -> builder.month(checkStateNotNull(matcher.group(1))), |
| 50 | + YEAR, (builder, matcher) -> builder.year(checkStateNotNull(matcher.group(1))), |
| 51 | + TRUNCATE, |
| 52 | + (builder, matcher) -> |
| 53 | + builder.truncate( |
| 54 | + checkStateNotNull(matcher.group(1)), |
| 55 | + Integer.parseInt(checkStateNotNull(matcher.group(2)))), |
| 56 | + BUCKET, |
| 57 | + (builder, matcher) -> |
| 58 | + builder.bucket( |
| 59 | + checkStateNotNull(matcher.group(1)), |
| 60 | + Integer.parseInt(checkStateNotNull(matcher.group(2)))), |
| 61 | + VOID, (builder, matcher) -> builder.alwaysNull(checkStateNotNull(matcher.group(1))), |
| 62 | + IDENTITY, |
| 63 | + (builder, matcher) -> builder.identity(checkStateNotNull(matcher.group(1)))); |
| 64 | + |
| 65 | + static PartitionSpec toPartitionSpec( |
| 66 | + List<String> fields, org.apache.beam.sdk.schemas.Schema beamSchema) { |
| 67 | + Schema schema = IcebergUtils.beamSchemaToIcebergSchema(beamSchema); |
| 68 | + PartitionSpec.Builder builder = PartitionSpec.builderFor(schema); |
| 69 | + |
| 70 | + for (String field : fields) { |
| 71 | + boolean matched = false; |
| 72 | + for (Map.Entry<Pattern, BiFunction<PartitionSpec.Builder, Matcher, PartitionSpec.Builder>> |
| 73 | + entry : TRANSFORMATIONS.entrySet()) { |
| 74 | + Matcher matcher = entry.getKey().matcher(field); |
| 75 | + if (matcher.find()) { |
| 76 | + builder = entry.getValue().apply(builder, matcher); |
| 77 | + matched = true; |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + if (!matched) { |
| 82 | + throw new IllegalArgumentException( |
| 83 | + "Could not find a partition transform for '" + field + "'."); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return builder.build(); |
| 88 | + } |
| 89 | +} |
0 commit comments