szehon-ho commented on code in PR #4672: URL: https://github.com/apache/iceberg/pull/4672#discussion_r862042346
########## api/src/main/java/org/apache/iceberg/expressions/ExpressionUtil.java: ########## @@ -0,0 +1,257 @@ +/* + * 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.iceberg.expressions; + +import java.util.regex.Pattern; +import java.util.stream.Collectors; +import org.apache.iceberg.transforms.Transform; +import org.apache.iceberg.transforms.Transforms; +import org.apache.iceberg.types.Types; + +/** + * Expression utility methods. + */ +public class ExpressionUtil { + private static final Transform<CharSequence, Integer> HASH_FUNC = Transforms + .bucket(Types.StringType.get(), Integer.MAX_VALUE); + private static final Pattern DATE = Pattern.compile("\\d\\d\\d\\d-\\d\\d-\\d\\d"); + private static final Pattern TIME = Pattern.compile( + "\\d\\d:\\d\\d(:\\d\\d(.\\d{1,6})?)?"); + private static final Pattern TIMESTAMP = Pattern.compile( + "\\d\\d\\d\\d-\\d\\d-\\d\\dT\\d\\d:\\d\\d(:\\d\\d(.\\d{1,6})?)?([-+]\\d\\d:\\d\\d)?"); + + private ExpressionUtil() { + } + + /** + * Produces an unbound {@link Expression} with the same structure, but with data values replaced by descriptions. + * <p> + * Numbers are replaced with magnitude and type, string-like values are replaced by hashes, and date/time values are + * replaced by the type. + * + * @param expr an Expression to sanitize + * @return a sanitized Expression + */ + public static Expression sanitize(Expression expr) { + return ExpressionVisitors.visit(expr, ExpressionSanitizer.INSTANCE); + } + + /** + * Produces a sanitized expression string with the same structure, but with data values replaced by descriptions. + * <p> + * Numbers are replaced with magnitude and type, string-like values are replaced by hashes, and date/time values are + * replaced by the type. + * + * @param expr an Expression to sanitize + * @return a sanitized expression string + */ + public static String toSanitizedString(Expression expr) { + return ExpressionVisitors.visit(expr, StringSanitizer.INSTANCE); + } + + private static class ExpressionSanitizer extends ExpressionVisitors.ExpressionVisitor<Expression> { + private static final ExpressionSanitizer INSTANCE = new ExpressionSanitizer(); + + @Override + public Expression alwaysTrue() { + return Expressions.alwaysTrue(); Review Comment: For another pr, but might be nice to have these boilerplate codes (and the string ones) in a base class at some point to reduce chance of errors, seems they show up a few places in the code -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
