-
Notifications
You must be signed in to change notification settings - Fork 1.4k
[core][format][spark] Support nested field predicate pushdown #9423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
1bd61cd
8f7cb7c
247f824
c4bb56a
79eeafe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,223 @@ | ||
| /* | ||
| * 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.paimon.predicate; | ||
|
|
||
| import org.apache.paimon.data.InternalRow; | ||
| import org.apache.paimon.types.DataField; | ||
| import org.apache.paimon.types.DataType; | ||
| import org.apache.paimon.types.RowType; | ||
|
|
||
| import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonCreator; | ||
| import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonIgnore; | ||
| import org.apache.paimon.shade.jackson2.com.fasterxml.jackson.annotation.JsonProperty; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| import static org.apache.paimon.utils.InternalRowUtils.get; | ||
| import static org.apache.paimon.utils.Preconditions.checkArgument; | ||
|
|
||
| /** | ||
| * Transform that extracts a field nested inside a row-typed column, for example {@code addr.city}. | ||
| * | ||
| * <p>The transform keeps the enclosing top-level column as its only {@link #inputs() input}, so | ||
| * anything that rewrites field indices (schema projection, for instance) keeps working without | ||
| * knowing about nesting. The positions below that column are held separately in {@link #path()}. | ||
| * | ||
| * <p>Deliberately <b>not</b> a {@link FieldTransform}: {@link LeafPredicate#fieldRefOptional()} | ||
| * returns empty for it, which is what keeps every consumer that equates a leaf with a top-level | ||
| * column — min/max pruning, file index lookup, ORC pushdown, schema evolution — from silently | ||
| * reading the enclosing column's metadata as if it belonged to the nested field. Those consumers | ||
| * give up on this transform instead, which costs pruning but never rows. | ||
| */ | ||
| public class NestedFieldTransform implements Transform { | ||
|
|
||
| private static final long serialVersionUID = 1L; | ||
|
|
||
| public static final String NAME = "NESTED_FIELD_REF"; | ||
|
|
||
| public static final String FIELD_FIELD_REF = "fieldRef"; | ||
| public static final String FIELD_PATH = "path"; | ||
|
|
||
| /** The top-level row-typed column the nested field lives in. */ | ||
| private final FieldRef fieldRef; | ||
|
|
||
| /** | ||
| * Names of the fields to descend into, relative to {@code fieldRef}'s row type. Never empty. | ||
| * | ||
| * <p>Deliberately names rather than positions: {@link #copyWithNewInputs} may be handed a | ||
| * structurally different row type — column masking and row filters remap that way — and a bare | ||
| * position would stay in range while silently addressing whatever now sits there. Names are | ||
| * re-resolved on every remap, so a reference either finds the same field or fails. | ||
| */ | ||
| private final List<String> path; | ||
|
|
||
| /** {@link #path} resolved to positions against {@code fieldRef}'s row type. */ | ||
| private final int[] positions; | ||
|
|
||
| /** Stable field ids of every component in {@link #path}. */ | ||
| private final int[] fieldIds; | ||
|
|
||
| private final String name; | ||
| private final DataType outputType; | ||
|
|
||
| @JsonCreator | ||
| public NestedFieldTransform( | ||
| @JsonProperty(FIELD_FIELD_REF) FieldRef fieldRef, | ||
| @JsonProperty(FIELD_PATH) List<String> path) { | ||
| this(fieldRef, path, null); | ||
| } | ||
|
|
||
| private NestedFieldTransform(FieldRef fieldRef, List<String> path, int[] expectedFieldIds) { | ||
| checkArgument(path != null && !path.isEmpty(), "Nested field path must not be empty."); | ||
| checkArgument( | ||
| expectedFieldIds == null || expectedFieldIds.length == path.size(), | ||
| "Nested field path and field ids must have the same size."); | ||
| this.fieldRef = fieldRef; | ||
| this.path = Collections.unmodifiableList(new ArrayList<>(path)); | ||
| this.positions = new int[this.path.size()]; | ||
| this.fieldIds = new int[this.path.size()]; | ||
|
|
||
| StringBuilder nameBuilder = new StringBuilder(fieldRef.name()); | ||
| DataType current = fieldRef.type(); | ||
| for (int i = 0; i < this.path.size(); i++) { | ||
| checkArgument( | ||
| current instanceof RowType, | ||
| "Nested field path of '%s' descends into a non-row type %s.", | ||
| fieldRef.name(), | ||
| current); | ||
| RowType rowType = (RowType) current; | ||
| String component = this.path.get(i); | ||
| int position = rowType.getFieldIndex(component); | ||
| checkArgument( | ||
| position >= 0, | ||
| "Nested field '%s' does not contain a field named '%s'.", | ||
| nameBuilder, | ||
| component); | ||
| DataField field = rowType.getFields().get(position); | ||
| if (expectedFieldIds != null) { | ||
| checkArgument( | ||
| field.id() == expectedFieldIds[i], | ||
| "Nested field '%s.%s' changed identity from field id %s to %s.", | ||
| nameBuilder, | ||
| component, | ||
| expectedFieldIds[i], | ||
| field.id()); | ||
| } | ||
| positions[i] = position; | ||
| fieldIds[i] = field.id(); | ||
| nameBuilder.append('.').append(component); | ||
| current = field.type(); | ||
| } | ||
| this.name = nameBuilder.toString(); | ||
| this.outputType = current; | ||
| } | ||
|
|
||
| @Override | ||
| public String name() { | ||
| return NAME; | ||
| } | ||
|
|
||
| @JsonProperty(FIELD_FIELD_REF) | ||
| public FieldRef fieldRef() { | ||
| return fieldRef; | ||
| } | ||
|
|
||
| @JsonProperty(FIELD_PATH) | ||
| public List<String> path() { | ||
| return path; | ||
| } | ||
|
|
||
| /** Dot-separated name from the top-level column down to the nested field, {@code addr.city}. */ | ||
| @JsonIgnore | ||
| public String fieldName() { | ||
| return name; | ||
| } | ||
|
|
||
| @Override | ||
| @JsonIgnore | ||
| public List<Object> inputs() { | ||
| return Collections.singletonList(fieldRef); | ||
| } | ||
|
|
||
| @Override | ||
| @JsonIgnore | ||
| public DataType outputType() { | ||
| return outputType; | ||
| } | ||
|
|
||
| /** | ||
| * Reads the nested field out of {@code row}, which must match the row type {@link #fieldRef} | ||
| * was built against. A null anywhere along the path yields null, matching SQL semantics for | ||
| * field access on a null struct. | ||
| */ | ||
| @Override | ||
| public Object transform(InternalRow row) { | ||
| int position = fieldRef.index(); | ||
| if (row.isNullAt(position)) { | ||
| return null; | ||
| } | ||
| RowType currentType = (RowType) fieldRef.type(); | ||
| InternalRow current = row.getRow(position, currentType.getFieldCount()); | ||
|
|
||
| for (int i = 0; i < positions.length - 1; i++) { | ||
| position = positions[i]; | ||
| if (current.isNullAt(position)) { | ||
| return null; | ||
| } | ||
| RowType nextType = (RowType) currentType.getTypeAt(position); | ||
| current = current.getRow(position, nextType.getFieldCount()); | ||
| currentType = nextType; | ||
| } | ||
|
|
||
| int leaf = positions[positions.length - 1]; | ||
| return get(current, leaf, currentType.getTypeAt(leaf)); | ||
| } | ||
|
|
||
| @Override | ||
| public Transform copyWithNewInputs(List<Object> inputs) { | ||
| checkArgument(inputs.size() == 1); | ||
| return new NestedFieldTransform((FieldRef) inputs.get(0), path, fieldIds); | ||
| } | ||
|
|
||
| private Object readResolve() { | ||
| return fieldIds == null ? new NestedFieldTransform(fieldRef, path) : this; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| NestedFieldTransform that = (NestedFieldTransform) o; | ||
| return Objects.equals(fieldRef, that.fieldRef) && Objects.equals(path, that.path); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(fieldRef, path); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return name; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -271,7 +271,9 @@ public Predicate in(int idx, List<Object> literals) { | |
| public Predicate in(Transform transform, List<Object> literals) { | ||
| // In the IN predicate, 20 literals are critical for performance. | ||
| // If there are more than 20 literals, the performance will decrease. | ||
| if (literals.size() > 20) { | ||
| // An empty list has no equals to OR together, so it must also take this branch - mirroring | ||
| // in(int, List) - rather than fall into or(emptyList()), which throws. | ||
| if (literals.size() > 20 || literals.isEmpty()) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Keep empty Transform sets from reaching parquet-mr This fixes builder evaluation, but it now lets an empty In/NotIn leaf reach the Parquet consumer. Both ParquetFilters.visitIn and visitNotIn call FilterApi with an empty Set; parquet-mr 1.16 rejects that in SetColumnFilterPredicate with IllegalArgumentException, and ParquetFilters.convert catches only UnsupportedOperationException. I reproduced both nested IN(empty) and NOT IN(empty) failing while creating the Parquet reader, so the new overload still cannot be used end to end with a dynamically empty literal list. Please have the Parquet visitor treat empty sets as unsupported, preserving residual evaluation, and add nested Parquet reader regressions for both forms.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed, reproduced both forms. Tests:
|
||
| return LeafPredicate.of(transform, In.INSTANCE, literals); | ||
| } | ||
|
|
||
|
|
@@ -286,6 +288,10 @@ public Predicate notIn(int idx, List<Object> literals) { | |
| return in(idx, literals).negate().get(); | ||
| } | ||
|
|
||
| public Predicate notIn(Transform transform, List<Object> literals) { | ||
| return in(transform, literals).negate().get(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [P2] Preserve empty-set semantics for Transform NOT IN This new overload inherits an empty-list failure from in(Transform, ...): unlike in(int, ...), that helper does not special-case empty input and calls or(empty), which throws. Therefore notIn(transform, emptyList()) raises IllegalArgumentException while notIn(idx, emptyList()) returns a valid NotIn predicate. Please align the empty-set handling, preferably in the Transform IN helper, and add an empty-list regression test for this overload.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed.
Test: |
||
| } | ||
|
|
||
| public Predicate between(int idx, Object includedLowerBound, Object includedUpperBound) { | ||
| DataField field = rowType.getFields().get(idx); | ||
| return new LeafPredicate( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Re-resolve nested identity when inputs are remapped
This preserves an ordinal path even when the replacement FieldRef has a different nested RowType. Nested transforms are now JSON-serializable and can be used by REST row filters, so a policy on info.secret with path [0] against ROW<secret, region> can be remapped against a Spark-pruned ROW and silently evaluate info.region instead. With same-typed fields this does not fail closed and can admit unauthorized rows. Please persist stable nested names or field IDs and re-resolve them during remapping, while ensuring auth reads the full nested dependencies; alternatively, reject nested transforms in row filters until their identity can be preserved.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, and I reproduced it: remapping a transform on
info.secretonto a prunedROW<region>silently producedinfo.region. Storing a bare position was my mistake — I had thought about the index moving but not about the row type itself changing shape.Took the first option you offered. The path is now the ordered component names rather than positions, and
copyWithNewInputsre-resolves them against the replacement row type, so a pruned-away leaf fails closed and a reordered row type still addresses the same field. Positions are derived once in the constructor and used only for evaluation.On the "while ensuring auth reads the full nested dependencies" part — I have not done that. Keeping
info.secretin the read schema when a row filter references it means touching the projection layer, and I was not sure that belonged in this PR. What is guaranteed now is that the case fails loudly instead of resolving elsewhere: the exception propagates out ofTableQueryAuthResult.remapPredicateand no caller catches it (AbstractDataTableScan:134). If you would rather have the dependency actually pulled into the projection, please say so — I am glad to do it here or in a follow-up, whichever you prefer.Tests at the auth entry point, since that is the path you were pointing at:
TableQueryAuthResultTest.testNestedRowFilterDoesNotDriftWhenTheLeafIsPrunedTableQueryAuthResultTest.testNestedRowFilterFollowsTheFieldWhenPositionsShiftand at the transform level,
NestedFieldTransformTest.testRemapOntoAPrunedRowTypeDoesNotDrift/testRemapFollowsTheFieldWhenPositionsShift. The second one is there to keep me honest: a validation that simply throws would pass the first test but fail this one, since a reordered row type has to resolve to the original field.In case it is useful for judging the blast radius, I also checked the other two
copyWithNewInputscallers:PredicateProjectionConverterandPartitionValuePredicateVisitorboth passfieldRef.type()through unchanged and only remap the top-level index, so neither could drift.TableQueryAuthResultis the one caller that re-derives the type from a different row type.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in 247f824.
NestedFieldTransformnow captures the stable field ID of every nested path component when the rule is created. During remapping it still resolves components by name so a reorder with unchanged IDs is supported, but it now verifies the IDs as well. A dropped-and-re-added same-name leaf therefore fails closed instead of binding a current rule to historical data.Added coverage for:
DROP info.secret/ADD info.secret, where the current schema has leaf ID 4 and the historical file has ID 2;The nested auth dependency widening part remains a separate fail-closed limitation: if a rule requires a leaf that the query projected away, the read is still rejected rather than automatically widening a partial nested projection.
Targeted Common/Core/Parquet suites pass locally, and CI has been restarted for the rebased branch.