laskoviymishka commented on code in PR #16131: URL: https://github.com/apache/iceberg/pull/16131#discussion_r3685544550
########## core/src/main/java/org/apache/iceberg/rest/restrictions/ReadRestrictionsParser.java: ########## @@ -0,0 +1,99 @@ +/* + * 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.rest.restrictions; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.util.List; +import org.apache.iceberg.expressions.Expression; +import org.apache.iceberg.expressions.ExpressionParser; +import org.apache.iceberg.functions.IcebergFunction; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.util.JsonUtil; + +public class ReadRestrictionsParser { + + private ReadRestrictionsParser() {} + + private static final String REQUIRED_ROW_FILTER = "required-row-filter"; + private static final String REQUIRED_COLUMN_PROJECTIONS = "required-column-projections"; + + public static String toJson(ReadRestrictions restrictions) { + return toJson(restrictions, false); + } + + public static String toJson(ReadRestrictions restrictions, boolean pretty) { + return JsonUtil.generate(gen -> toJson(restrictions, gen), pretty); + } + + public static void toJson(ReadRestrictions restrictions, JsonGenerator generator) + throws IOException { + Preconditions.checkArgument(restrictions != null, "Invalid read restrictions: null"); + + generator.writeStartObject(); + + if (restrictions.rowFilter() != null) { + generator.writeFieldName(REQUIRED_ROW_FILTER); + ExpressionParser.toJson(restrictions.rowFilter(), generator); Review Comment: tracked — #13879 is landing on field-id refs for exactly this reason, so this is just the prototype trailing the spec. nothing to change here until the spec firms up. resolving. ########## api/src/main/java/org/apache/iceberg/functions/MaskToFixedValue.java: ########## @@ -0,0 +1,211 @@ +/* + * 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.math.BigDecimal; +import java.math.BigInteger; +import java.nio.ByteBuffer; +import java.nio.ByteOrder; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.Collections; +import java.util.List; +import java.util.UUID; +import org.apache.iceberg.StructLike; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.types.Type; +import org.apache.iceberg.types.Types; +import org.apache.iceberg.util.DateTimeUtil; +import org.apache.iceberg.util.SerializableFunction; +import org.apache.iceberg.variants.Variant; + +/** Returns a spec-defined fixed value for the column's type. */ +public final class MaskToFixedValue extends IcebergFunction.BaseFunction<Object, Object> { + + private static final Integer INT_DEFAULT = 0; + private static final Long LONG_DEFAULT = 0L; + private static final Float FLOAT_DEFAULT = 0.0f; + private static final Double DOUBLE_DEFAULT = 0.0d; + // Per spec: string uses "XXXXXXXX" (not "") so masked strings stay visually distinct from + // legitimately empty strings. All other types use the zero value of their representation. + private static final String STRING_DEFAULT = "XXXXXXXX"; + private static final Integer DATE_DEFAULT = DateTimeUtil.daysFromDate(LocalDate.of(1970, 1, 1)); + private static final Long TIME_DEFAULT_MICROS = 0L; + private static final Long TIMESTAMP_DEFAULT_MICROS = + DateTimeUtil.microsFromTimestamp(LocalDateTime.of(1970, 1, 1, 0, 0)); + private static final Long TIMESTAMP_DEFAULT_NANOS = + DateTimeUtil.nanosFromTimestamp(LocalDateTime.of(1970, 1, 1, 0, 0)); + private static final UUID UUID_DEFAULT = UUID.fromString("00000000-0000-0000-0000-000000000000"); + private static final ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0).asReadOnlyBuffer(); + + // Empty Variant: V1 metadata with no entries + empty object value. + private static final Variant EMPTY_VARIANT = + Variant.from( + ByteBuffer.wrap(new byte[] {0x01, 0x00, 0x00, 0x02, 0x00, 0x00}) + .order(ByteOrder.LITTLE_ENDIAN)); + + public MaskToFixedValue(int fieldId) { + super(fieldId); + } + + @Override + public String name() { + return MASK_TO_FIXED_VALUE; + } + + @Override + public boolean canBind(Type type) { + switch (type.typeId()) { + case BOOLEAN: + case INTEGER: + case LONG: + case FLOAT: + case DOUBLE: + case STRING: + case DATE: + case TIME: + case TIMESTAMP: + case TIMESTAMP_NANO: + case UUID: + case FIXED: + case BINARY: + case DECIMAL: + case VARIANT: + case LIST: + case MAP: + case STRUCT: + return true; + default: + return false; + } + } + + @Override + public SerializableFunction<Object, Object> bind(Type type) { + Preconditions.checkArgument( + canBind(type), "mask-to-fixed-value is not supported for type: %s", type); + Object defaultValue = defaultValueFor(type); + return defaultValue instanceof ByteBuffer + ? new ConstantByteBufferFn((ByteBuffer) defaultValue) + : new ConstantFn(defaultValue); + } + + private static Object defaultValueFor(Type type) { + switch (type.typeId()) { + case BOOLEAN: + return Boolean.FALSE; + case INTEGER: + return INT_DEFAULT; + case LONG: + return LONG_DEFAULT; + case FLOAT: + return FLOAT_DEFAULT; + case DOUBLE: + return DOUBLE_DEFAULT; + case STRING: + return STRING_DEFAULT; + case DATE: + return DATE_DEFAULT; + case TIME: + return TIME_DEFAULT_MICROS; + case TIMESTAMP: + return TIMESTAMP_DEFAULT_MICROS; + case TIMESTAMP_NANO: + return TIMESTAMP_DEFAULT_NANOS; + case UUID: + return UUID_DEFAULT; + case FIXED: + return ByteBuffer.allocate(((Types.FixedType) type).length()).asReadOnlyBuffer(); + case BINARY: + return EMPTY_BUFFER; + case DECIMAL: + return new BigDecimal(BigInteger.ZERO, ((Types.DecimalType) type).scale()); + case VARIANT: + return EMPTY_VARIANT; + case LIST: + return Collections.emptyList(); + case MAP: + return Collections.emptyMap(); + case STRUCT: + return defaultStruct(type.asStructType()); + default: + throw new IllegalStateException("unreachable: canBind should have rejected " + type); + } + } + + private static StructLike defaultStruct(Types.StructType structType) { + List<Types.NestedField> fields = structType.fields(); + Object[] values = new Object[fields.size()]; + for (int i = 0; i < fields.size(); i++) { + values[i] = defaultValueFor(fields.get(i).type()); + } + return new DefaultStruct(values); + } + + private static final class ConstantFn implements SerializableFunction<Object, Object> { + private final Object constant; + + ConstantFn(Object constant) { + this.constant = constant; + } + + @Override + public Object apply(Object value) { + return constant; Review Comment: answered by the spec — the yaml says mask-to-fixed-value replaces null with the fixed value and explicitly doesn't preserve it, so the code matches intent. only leftover is getting the javadoc + test to state that as the intended behavior rather than an edge case. resolving; the null-leaks-non-null point, if it matters, belongs on #13879. ########## core/src/main/java/org/apache/iceberg/rest/RESTSessionCatalog.java: ########## @@ -557,13 +558,22 @@ public Table loadTable(SessionContext context, TableIdentifier identifier) { } List<Credential> credentials = response.credentials(); + ReadRestrictions readRestrictions = response.readRestrictions(); RESTClient tableClient = client.withAuthSession(tableSession); Supplier<BaseTable> tableSupplier = createTableSupplier( - finalIdentifier, tableMetadata, context, tableClient, tableConf, credentials); + finalIdentifier, + tableMetadata, + context, + tableClient, + tableConf, + credentials, + readRestrictions); String eTag = responseHeaders.getOrDefault(HttpHeaders.ETAG, null); - if (eTag != null) { + if (eTag != null && readRestrictions.isEmpty()) { Review Comment: agreed this is a productionization detail, not a prototype concern — folding a policy version into the cache key is the fix whenever it gets there. resolving. -- 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]
