-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Add mapping to Iceberg for external name-based schemas #338
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
96 changes: 96 additions & 0 deletions
96
core/src/main/java/org/apache/iceberg/mapping/MappedField.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,96 @@ | ||
| /* | ||
| * 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.mapping; | ||
|
|
||
| import com.google.common.base.Joiner; | ||
| import com.google.common.collect.ImmutableSet; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * An immutable mapping between a field ID and a set of names. | ||
| */ | ||
| public class MappedField { | ||
|
|
||
| static MappedField of(Integer id, String name) { | ||
| return new MappedField(id, ImmutableSet.of(name), null); | ||
| } | ||
|
|
||
| static MappedField of(Integer id, Iterable<String> names) { | ||
| return new MappedField(id, names, null); | ||
| } | ||
|
|
||
| static MappedField of(Integer id, String name, MappedFields nestedMapping) { | ||
| return new MappedField(id, ImmutableSet.of(name), nestedMapping); | ||
| } | ||
|
|
||
| static MappedField of(Integer id, Iterable<String> names, MappedFields nestedMapping) { | ||
| return new MappedField(id, names, nestedMapping); | ||
| } | ||
|
|
||
| private final Set<String> names; | ||
| private Integer id; | ||
| private MappedFields nestedMapping; | ||
|
|
||
| private MappedField(Integer id, Iterable<String> names, MappedFields nested) { | ||
| this.id = id; | ||
| this.names = ImmutableSet.copyOf(names); | ||
| this.nestedMapping = nested; | ||
| } | ||
|
|
||
| public Integer id() { | ||
| return id; | ||
| } | ||
|
|
||
| public Set<String> names() { | ||
| return names; | ||
| } | ||
|
|
||
| public MappedFields nestedMapping() { | ||
| return nestedMapping; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object other) { | ||
| if (this == other) { | ||
| return true; | ||
| } | ||
|
|
||
| if (other == null || getClass() != other.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| MappedField that = (MappedField) other; | ||
| return names.equals(that.names) && | ||
| Objects.equals(id, that.id) && | ||
| Objects.equals(nestedMapping, that.nestedMapping); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(names, id, nestedMapping); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "([" + Joiner.on(", ").join(names) + "] -> " + (id != null ? id : "?") + | ||
| (nestedMapping != null ? ", " + nestedMapping + ")" : ")"); | ||
| } | ||
| } |
110 changes: 110 additions & 0 deletions
110
core/src/main/java/org/apache/iceberg/mapping/MappedFields.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,110 @@ | ||
| /* | ||
| * 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.mapping; | ||
|
|
||
| import com.google.common.base.Joiner; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| public class MappedFields { | ||
|
|
||
| static MappedFields of(MappedField... fields) { | ||
| return new MappedFields(ImmutableList.copyOf(fields)); | ||
| } | ||
|
|
||
| static MappedFields of(List<MappedField> fields) { | ||
| return new MappedFields(fields); | ||
| } | ||
|
|
||
| private final List<MappedField> fields; | ||
| private final Map<String, Integer> nameToId; | ||
| private final Map<Integer, MappedField> idToField; | ||
|
|
||
| private MappedFields(List<MappedField> fields) { | ||
| this.fields = ImmutableList.copyOf(fields); | ||
| this.nameToId = indexIds(fields); | ||
| this.idToField = indexFields(fields); | ||
| } | ||
|
|
||
| public MappedField field(int id) { | ||
| return idToField.get(id); | ||
| } | ||
|
|
||
| public Integer id(String name) { | ||
| return nameToId.get(name); | ||
| } | ||
|
|
||
| public int size() { | ||
| return fields.size(); | ||
| } | ||
|
|
||
| private static Map<String, Integer> indexIds(List<MappedField> fields) { | ||
| ImmutableMap.Builder<String, Integer> builder = ImmutableMap.builder(); | ||
| fields.forEach(field -> | ||
| field.names().forEach(name -> { | ||
| Integer id = field.id(); | ||
| if (id != null) { | ||
| builder.put(name, id); | ||
| } | ||
| })); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| private static Map<Integer, MappedField> indexFields(List<MappedField> fields) { | ||
| ImmutableMap.Builder<Integer, MappedField> builder = ImmutableMap.builder(); | ||
| fields.forEach(field -> { | ||
| Integer id = field.id(); | ||
| if (id != null) { | ||
| builder.put(id, field); | ||
| } | ||
| }); | ||
| return builder.build(); | ||
| } | ||
|
|
||
| public List<MappedField> fields() { | ||
| return fields; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object other) { | ||
| if (this == other) { | ||
| return true; | ||
| } | ||
|
|
||
| if (other == null || getClass() != other.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| return fields.equals(((MappedFields) other).fields); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(fields); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[ " + Joiner.on(", ").join(fields) + " ]"; | ||
| } | ||
| } |
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.
We're calling this default, but it's not really a default, right? Shouldn't this just be
schema.name-mapping?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.
The idea is that there may eventually be multiple mappings. If you have two streams of data written by Kafka, for example, you may want a different mapping from source name to Iceberg columns.
The current name defines a default mapping, to be used when a mapping is needed but there is no specified mapping for a file. Later, we can add
schema.name-mapping.(name)properties to add more than one.