ahmedabu98 commented on code in PR #40749: URL: https://github.com/apache/beam/pull/40749#discussion_r4383320966
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/PathPattern.java: ########## @@ -0,0 +1,236 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.beam.sdk.io.iceberg; + +import static org.apache.beam.sdk.util.Preconditions.checkArgumentNotNull; +import static org.apache.beam.sdk.util.Preconditions.checkStateNotNull; +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkArgument; +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState; + +import java.io.Serializable; +import java.util.ArrayList; +import java.util.Collections; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; +import org.apache.beam.sdk.io.FileSystemUtils; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Matches a file path against a glob with {@code {name}} placeholders, and reports what each + * placeholder captured. + * + * <p>Pattern {@code gs://lake/export/{table}/**} over path {@code + * gs://lake/export/orders/part-0.parquet} captures {@code table=orders}. The whole path has to + * match, so the pattern decides both which table a file belongs to and whether it belongs at all. + */ +final class PathPattern implements Serializable { + private static final Pattern PLACEHOLDER = Pattern.compile("\\{(.+?)}"); + + /** A name must survive rendering into a dotted table template: no dots, slashes or blanks. */ + private static final Pattern NAME = Pattern.compile("[A-Za-z0-9_-]+"); + + private final Pattern regex; + private final List<String> captureNames; + + private PathPattern(Pattern regex, List<String> captureNames) { + this.regex = regex; + this.captureNames = Collections.unmodifiableList(captureNames); + } + + /** + * Translates the path pattern to a regex, each placeholder becoming a capturing group. {@code + * gs://lake/{db}/{tbl}/**} becomes {@code gs://lake/([^/]+)/([^/]+)/.*}, which a path has to + * match in full. + */ + static PathPattern compile(String pathPattern) { + List<String> names = placeholders(pathPattern); + checkNamesAreBindable(names, pathPattern); + // the -1 keeps a trailing empty part, so "gs://lake/{tbl}" still yields a group + String[] fixedParts = PLACEHOLDER.split(pathPattern, -1); + checkNoDoubleStarAbovePlaceholders(fixedParts, pathPattern); + checkArgument( + !pathPattern.endsWith("/"), + "'%s' ends with '/', so it names a folder and matches no file; end it with a selector for" + + " the files, such as '**'.", + pathPattern); + Pattern regex = Pattern.compile(toRegex(fixedParts)); + checkState( + regex.matcher("").groupCount() == names.size(), + "'%s' translated to a regex with %s groups for %s placeholders.", + pathPattern, + regex.matcher("").groupCount(), + names.size()); + return new PathPattern(regex, names); + } + + private static void checkNamesAreBindable(List<String> names, String pathPattern) { + for (int i = 0; i < names.size(); i++) { + String name = names.get(i); + checkArgument( + NAME.matcher(name).matches(), + "Placeholder '%s' in '%s' is not a name; use letters, digits, '_' or '-'.", + name, + pathPattern); + checkArgument( + names.indexOf(name) == i, + "Placeholder '%s' appears twice in '%s'; a name can only be captured once.", + name, + pathPattern); + } + } + + // A '**' crosses directory levels, so one above a placeholder leaves the folder that placeholder + // names undetermined: gs://lake/**/{tbl}/** over gs://lake/a/b/c/f.parquet binds tbl to c, but + // only because the regex is greedy. Only the text after the last placeholder may hold one. + private static void checkNoDoubleStarAbovePlaceholders(String[] fixedParts, String pathPattern) { + for (int i = 0; i < fixedParts.length - 1; i++) { + checkArgument( + !fixedParts[i].contains("**"), + "'%s' has a '**' above a placeholder, so the folder that placeholder names is not" + + " determined; a '**' can only come after the last placeholder.", + pathPattern); + } + } Review Comment: Fair point, I think we can go ahead with this -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
