RussellSpitzer commented on code in PR #16198: URL: https://github.com/apache/iceberg/pull/16198#discussion_r3269813655
########## api/src/main/java/org/apache/iceberg/functions/Action.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.functions; + +import java.io.Serializable; +import java.util.Objects; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.util.SerializableFunction; + +/** + * A column projection action from the ReadRestrictions spec. + * + * <p>{@link #bind(Type)} returns the masking {@link SerializableFunction} for a given column type; + * all bound functions return null for null input. Per spec all predefined actions preserve the + * input column type, so the bound function maps {@code T -> T}. + * + * @param <T> column value type + */ +public interface Action<T> extends Serializable { + + String MASK_ALPHANUM = "mask-alphanum"; + String MASK_TO_FIXED_VALUE = "mask-to-fixed-value"; + String REPLACE_WITH_NULL = "replace-with-null"; + String SHOW_FIRST_4 = "show-first-4"; + String SHOW_LAST_4 = "show-last-4"; + String TRUNCATE_TO_YEAR = "truncate-to-year"; + String TRUNCATE_TO_MONTH = "truncate-to-month"; + String SHA_256_GLOBAL = "sha-256-global"; + String SHA_256_QUERY_LOCAL = "sha-256-query-local"; + String APPLY_EXPRESSION = "apply-expression"; + + /** The action discriminator string as sent on the wire. */ + String actionType(); + + /** The field id of the column this action applies to. */ + int fieldId(); + + /** + * Returns a function that applies this action to values of the given {@link Type}. + * + * @throws IllegalArgumentException if the type is not supported by this action. + */ + default SerializableFunction<T, T> bind(Type type) { + throw new UnsupportedOperationException("bind is not implemented for " + getClass().getName()); + } + + /** + * Variant that accepts a per-query salt. Only {@link Sha256QueryLocal} uses the salt; other + * actions ignore it and delegate to {@link #bind(Type)}. + */ + default SerializableFunction<T, T> bind(Type type, byte[] salt) { + return bind(type); + } + + /** Returns true if this action can be bound to the given {@link Type}. */ + boolean canBind(Type type); + + /** Base for all concrete actions; holds the field id. */ + abstract class BaseAction<T> implements Action<T> { + private final int fieldId; + + BaseAction(int fieldId) { + this.fieldId = fieldId; + } + + @Override + public final int fieldId() { + return fieldId; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof Action)) { + return false; + } + Action<?> other = (Action<?>) o; Review Comment: Potentially dangerous. Quick example Action<?> u = new UnknownAction(1, "apply-expression"); Action<?> a = new ApplyExpression(1, someExpr); u.equals(a); // → true (uses BaseAction.equals: instanceof Action ✓, fieldId ✓, actionType "apply-expression" == "apply-expression" ✓) a.equals(u); // → false (uses ApplyExpression.equals: instanceof ApplyExpression ✗) Probably best to for class equivelence on line 92 instead of just instanceOf -- 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]
