-
Notifications
You must be signed in to change notification settings - Fork 2.9k
API: Move variant to API and add extract expression #12304
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
671b1be
API: Move Variant interfaces to API.
rdblue 253298b
API: Add extract expression for variants.
rdblue 6a0fdc0
Apply spotless and check for a bound variant.
rdblue ca7473a
Update JSON path parsing to conform to RFC 9535.
rdblue ffb1c00
Make PathUtil privage and remove VariantData (no longer used).
rdblue a8a90aa
Revert "API: Move Variant interfaces to API."
rdblue 6ad4a9c
Apply spotless.
rdblue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
api/src/main/java/org/apache/iceberg/expressions/BoundExtract.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| /* | ||
| * 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.StructLike; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Joiner; | ||
| import org.apache.iceberg.types.Type; | ||
|
|
||
| public class BoundExtract<T> implements BoundTerm<T> { | ||
| private final BoundReference<?> ref; | ||
| private final String path; | ||
| private final String fullFieldName; | ||
| private final Type type; | ||
|
|
||
| BoundExtract(BoundReference<?> ref, String path, Type type) { | ||
| this.ref = ref; | ||
| this.path = path; | ||
| this.fullFieldName = Joiner.on(".").join(PathUtil.parse(path)); | ||
| this.type = type; | ||
| } | ||
|
|
||
| @Override | ||
| public BoundReference<?> ref() { | ||
| return ref; | ||
| } | ||
|
|
||
| public String path() { | ||
| return path; | ||
| } | ||
|
|
||
| String fullFieldName() { | ||
| return fullFieldName; | ||
| } | ||
|
|
||
| @Override | ||
| public Type type() { | ||
| return type; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEquivalentTo(BoundTerm<?> other) { | ||
| if (other instanceof BoundExtract) { | ||
| BoundExtract<?> that = (BoundExtract<?>) other; | ||
| return ref.isEquivalentTo(that.ref) && path.equals(that.path) && type.equals(that.type); | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| @Override | ||
| public T eval(StructLike struct) { | ||
| throw new UnsupportedOperationException("Cannot evaluate " + this); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "extract(" + ref + ", path=" + path + ", type=" + type + ")"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
api/src/main/java/org/apache/iceberg/expressions/PathUtil.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 java.util.List; | ||
| import java.util.function.Predicate; | ||
| import java.util.regex.Pattern; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Splitter; | ||
|
|
||
| class PathUtil { | ||
| private PathUtil() {} | ||
|
|
||
| private static final String RFC9535_NAME_FIRST = | ||
| "[A-Za-z_\\x{0080}-\\x{D7FF}\\x{E000}-\\x{10FFFF}]"; | ||
| private static final String RFC9535_NAME_CHARS = | ||
| "[0-9A-Za-z_\\x{0080}-\\x{D7FF}\\x{E000}-\\x{10FFFF}]*"; | ||
| private static final Predicate<String> RFC9535_MEMBER_NAME_SHORTHAND = | ||
| Pattern.compile(RFC9535_NAME_FIRST + RFC9535_NAME_CHARS).asMatchPredicate(); | ||
|
|
||
| private static final Splitter DOT = Splitter.on("."); | ||
| private static final String ROOT = "$"; | ||
|
|
||
| static List<String> parse(String path) { | ||
| Preconditions.checkArgument(path != null, "Invalid path: null"); | ||
| Preconditions.checkArgument( | ||
| !path.contains("[") && !path.contains("]"), "Unsupported path, contains bracket: %s", path); | ||
| Preconditions.checkArgument( | ||
| !path.contains("*"), "Unsupported path, contains wildcard: %s", path); | ||
| Preconditions.checkArgument( | ||
| !path.contains(".."), "Unsupported path, contains recursive descent: %s", path); | ||
|
|
||
| List<String> parts = DOT.splitToList(path); | ||
| Preconditions.checkArgument( | ||
| ROOT.equals(parts.get(0)), "Invalid path, does not start with %s: %s", ROOT, path); | ||
|
|
||
| List<String> names = parts.subList(1, parts.size()); | ||
| for (String name : names) { | ||
| Preconditions.checkArgument( | ||
| RFC9535_MEMBER_NAME_SHORTHAND.test(name), | ||
| "Invalid path: %s (%s has invalid characters)", | ||
| path, | ||
| name); | ||
| } | ||
|
|
||
| return names; | ||
| } | ||
| } |
67 changes: 67 additions & 0 deletions
67
api/src/main/java/org/apache/iceberg/expressions/UnboundExtract.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| /* | ||
| * 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.exceptions.ValidationException; | ||
| import org.apache.iceberg.types.Type; | ||
| import org.apache.iceberg.types.Types; | ||
|
|
||
| public class UnboundExtract<T> implements UnboundTerm<T> { | ||
| private final NamedReference<?> ref; | ||
| private final String path; | ||
| private final Type.PrimitiveType type; | ||
|
|
||
| public UnboundExtract(NamedReference<?> ref, String path, String type) { | ||
| this.ref = ref; | ||
| this.path = path; | ||
| this.type = Types.fromPrimitiveString(type); | ||
| // verify that the path is well-formed | ||
| PathUtil.parse(path); | ||
| } | ||
|
|
||
| @Override | ||
| public BoundTerm<T> bind(Types.StructType struct, boolean caseSensitive) { | ||
| BoundReference<?> boundRef = ref.bind(struct, caseSensitive); | ||
| ValidationException.check( | ||
| Types.VariantType.get().equals(boundRef.type()), | ||
| "Cannot bind extract, not a variant: %s", | ||
| boundRef.name()); | ||
| ValidationException.check( | ||
| !type.equals(Types.UnknownType.get()), "Invalid type to extract: unknown"); | ||
| return new BoundExtract<>(boundRef, path, type); | ||
| } | ||
|
|
||
| @Override | ||
| public NamedReference<?> ref() { | ||
| return ref; | ||
| } | ||
|
|
||
| public String path() { | ||
| return path; | ||
| } | ||
|
|
||
| public Type type() { | ||
| return type; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "extract(" + ref + ", path=" + path + ", type=" + type + ")"; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I see this change is same as the one in
BoundReference(field.isOptional()). ForBoundTransform, seems we added!transform.preservesOrder()- which transforms produce nulls when not order-preserving?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.
A good example is
VoidTransform, which maps all values to null. If a transform preserves order then it can't produce a null value for a non-null input because that would violate order preservation. If it does not preserve order, then it could map values to null so we account for that. If a field is required there are no non-null values, so the only way a null could be produced is by the transform.