-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API: Move variant to API and add extract expression (#12304)
- Loading branch information
Showing
10 changed files
with
522 additions
and
3 deletions.
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 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 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 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 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 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 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 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 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.