-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix struct comparison and add struct and partition set implementations #1307
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| /* | ||
| * 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.types; | ||
|
|
||
| import java.util.Objects; | ||
|
|
||
| @FunctionalInterface | ||
| public interface JavaHash<T> { | ||
| int hash(T value); | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| static <T> JavaHash<T> forType(Type type) { | ||
| switch (type.typeId()) { | ||
| case STRING: | ||
| return (JavaHash<T>) JavaHashes.strings(); | ||
| case STRUCT: | ||
| return (JavaHash<T>) JavaHashes.struct(type.asStructType()); | ||
| case LIST: | ||
| return (JavaHash<T>) JavaHashes.list(type.asListType()); | ||
| default: | ||
| return Objects::hashCode; | ||
| } | ||
| } | ||
| } |
112 changes: 112 additions & 0 deletions
112
api/src/main/java/org/apache/iceberg/types/JavaHashes.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,112 @@ | ||
| /* | ||
| * 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.types; | ||
|
|
||
| import java.util.List; | ||
| import java.util.function.IntFunction; | ||
| import org.apache.iceberg.StructLike; | ||
|
|
||
| public class JavaHashes { | ||
| private JavaHashes() { | ||
| } | ||
|
|
||
| public static int hashCode(CharSequence str) { | ||
| int result = 177; | ||
| for (int i = 0; i < str.length(); i += 1) { | ||
| char ch = str.charAt(i); | ||
| result = 31 * result + (int) ch; | ||
| } | ||
| return result; | ||
| } | ||
|
|
||
| static JavaHash<Object> strings() { | ||
| return CharSequenceHash.INSTANCE; | ||
| } | ||
|
|
||
| static JavaHash<StructLike> struct(Types.StructType struct) { | ||
| return new StructLikeHash(struct); | ||
| } | ||
|
|
||
| static JavaHash<List<?>> list(Types.ListType list) { | ||
| return new ListHash(list); | ||
| } | ||
|
|
||
| private static class CharSequenceHash implements JavaHash<Object> { | ||
| private static final CharSequenceHash INSTANCE = new CharSequenceHash(); | ||
|
|
||
| private CharSequenceHash() { | ||
| } | ||
|
|
||
| @Override | ||
| public int hash(Object str) { | ||
| if (str instanceof CharSequence) { | ||
| return JavaHashes.hashCode((CharSequence) str); | ||
| } else if (str != null) { | ||
| // UnknownTransform results are assumed to be string, the most generic type. But there is no guarantee that the | ||
| // values actually are strings so this can receive non-string values to hash. To get a consistent hash code for | ||
| // those values, convert to string an hash the string. | ||
| return JavaHashes.hashCode(str.toString()); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| private static class StructLikeHash implements JavaHash<StructLike> { | ||
| private final JavaHash<Object>[] hashes; | ||
|
|
||
| private StructLikeHash(Types.StructType struct) { | ||
| this.hashes = struct.fields().stream() | ||
| .map(Types.NestedField::type) | ||
| .map(JavaHash::forType) | ||
| .toArray((IntFunction<JavaHash<Object>[]>) JavaHash[]::new); | ||
| } | ||
|
|
||
| @Override | ||
| public int hash(StructLike struct) { | ||
| int result = 97; | ||
| int len = hashes.length; | ||
| result = 41 * result + len; | ||
| for (int i = 0; i < len; i += 1) { | ||
| result = 41 * result + hashes[i].hash(struct.get(i, Object.class)); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
|
|
||
| private static class ListHash implements JavaHash<List<?>> { | ||
| private final JavaHash<Object> elementHash; | ||
|
|
||
| private ListHash(Types.ListType list) { | ||
| this.elementHash = JavaHash.forType(list.elementType()); | ||
| } | ||
|
|
||
| @Override | ||
| public int hash(List<?> list) { | ||
| int result = 17; | ||
| int len = list.size(); | ||
| result = 37 * result + len; | ||
| for (int i = 0; i < len; i += 1) { | ||
| result = 37 * result + elementHash.hash(list.get(i)); | ||
| } | ||
| return result; | ||
| } | ||
| } | ||
| } |
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
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.
This was renamed to avoid a conflict with the new
PartitionSetclass, and updated to useStructLikeWrapperwith correctequals/hashCodeimplementations.