This is an automated email from the ASF dual-hosted git repository. singhpk234 pushed a commit to branch feature/serialize-bound-expression in repository https://gitbox.apache.org/repos/asf/iceberg.git
commit 91a7baeb5216451e785df607b0bb3a6f82544c86 Author: Prashant Singh <[email protected]> AuthorDate: Thu Sep 25 18:20:49 2025 -0700 POC: Try Resolved Reference --- .../apache/iceberg/expressions/Expressions.java | 15 +++++ .../iceberg/expressions/ResolvedReference.java | 64 ++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/api/src/main/java/org/apache/iceberg/expressions/Expressions.java b/api/src/main/java/org/apache/iceberg/expressions/Expressions.java index 78012def5a..1e93d8e75e 100644 --- a/api/src/main/java/org/apache/iceberg/expressions/Expressions.java +++ b/api/src/main/java/org/apache/iceberg/expressions/Expressions.java @@ -305,6 +305,21 @@ public class Expressions { return new NamedReference<>(name); } + /** + * Constructs a resolved reference for a given column. + * + * <p>The following are equivalent: equals("a", 5) and if a fieldId is 1 and equals(ref("a", 1), + * 5). + * + * @param name a column name + * @param fieldId the field ID of the column + * @param <T> the Java type of this reference + * @return a named reference + */ + public static <T> ResolvedReference<T> ref(String name, int fieldId) { + return new ResolvedReference<>(name, fieldId); + } + /** * Constructs a transform expression for a given column. * diff --git a/api/src/main/java/org/apache/iceberg/expressions/ResolvedReference.java b/api/src/main/java/org/apache/iceberg/expressions/ResolvedReference.java new file mode 100644 index 0000000000..b12bff5832 --- /dev/null +++ b/api/src/main/java/org/apache/iceberg/expressions/ResolvedReference.java @@ -0,0 +1,64 @@ +/* + * 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 org.apache.iceberg.Schema; +import org.apache.iceberg.exceptions.ValidationException; +import org.apache.iceberg.types.Types; + +public class ResolvedReference<T> implements UnboundTerm<T>, Reference<T> { + private final String name; + private final int fieldId; + + public ResolvedReference(String name, int fieldId) { + this.name = name; + this.fieldId = fieldId; + } + + @Override + public String name() { + return name; + } + + public int fieldId() { + return fieldId; + } + + @Override + public BoundTerm<T> bind(Types.StructType struct, boolean caseSensitive) { + // assumption is that we always have the field id + Schema schema = struct.asSchema(); + // we ignore case sensitivity here because field ids are unique + Types.NestedField field = schema.findField(fieldId); + ValidationException.check( + field != null, "Cannot find field '%s' in struct: %s", name, schema.asStruct()); + + return new BoundReference<>(field, schema.accessorForField(field.fieldId()), name); + } + + @Override + public NamedReference<?> ref() { + return new NamedReference<>(name); + } + + @Override + public String toString() { + return String.format("ref(name=\"%s\", fieldId=\"%s\")", name, fieldId); + } +}
