-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Core: add labels to the load table/view read path #17337
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
Open
laskoviymishka
wants to merge
1
commit into
apache:main
Choose a base branch
from
laskoviymishka:labels-client-impl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
37 changes: 37 additions & 0 deletions
37
core/src/main/java/org/apache/iceberg/rest/labels/FieldLabels.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,37 @@ | ||
| /* | ||
| * 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.rest.labels; | ||
|
|
||
| import java.util.Map; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.immutables.value.Value; | ||
|
|
||
| /** Labels attached to a single field, identified by its field id. See {@link Labels}. */ | ||
| @Value.Immutable | ||
| public interface FieldLabels { | ||
| int fieldId(); | ||
|
|
||
| Map<String, String> labels(); | ||
|
|
||
| @Value.Check | ||
| default void validate() { | ||
| Preconditions.checkArgument(fieldId() >= 1, "Invalid field id: must be >= 1"); | ||
| Preconditions.checkArgument(!labels().isEmpty(), "Invalid labels: must be non-empty"); | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/apache/iceberg/rest/labels/FieldLabelsParser.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.rest.labels; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.io.IOException; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.util.JsonUtil; | ||
|
|
||
| public class FieldLabelsParser { | ||
| private static final String FIELD_ID = "field-id"; | ||
| private static final String LABELS = "labels"; | ||
|
|
||
| private FieldLabelsParser() {} | ||
|
|
||
| public static String toJson(FieldLabels fieldLabels) { | ||
| return toJson(fieldLabels, false); | ||
| } | ||
|
|
||
| public static String toJson(FieldLabels fieldLabels, boolean pretty) { | ||
| return JsonUtil.generate(gen -> toJson(fieldLabels, gen), pretty); | ||
| } | ||
|
|
||
| public static void toJson(FieldLabels fieldLabels, JsonGenerator gen) throws IOException { | ||
| Preconditions.checkArgument(null != fieldLabels, "Invalid field labels: null"); | ||
|
|
||
| gen.writeStartObject(); | ||
|
|
||
| gen.writeNumberField(FIELD_ID, fieldLabels.fieldId()); | ||
| JsonUtil.writeStringMap(LABELS, fieldLabels.labels(), gen); | ||
|
|
||
| gen.writeEndObject(); | ||
| } | ||
|
|
||
| public static FieldLabels fromJson(String json) { | ||
| return JsonUtil.parse(json, FieldLabelsParser::fromJson); | ||
| } | ||
|
|
||
| public static FieldLabels fromJson(JsonNode json) { | ||
| Preconditions.checkArgument(null != json, "Cannot parse field labels from null object"); | ||
|
|
||
| return ImmutableFieldLabels.builder() | ||
| .fieldId(JsonUtil.getInt(FIELD_ID, json)) | ||
| .labels(JsonUtil.getStringMap(LABELS, json)) | ||
| .build(); | ||
| } | ||
| } |
45 changes: 45 additions & 0 deletions
45
core/src/main/java/org/apache/iceberg/rest/labels/Labels.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,45 @@ | ||
| /* | ||
| * 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.rest.labels; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.immutables.value.Value; | ||
|
|
||
| /** Optional catalog-provided labels returned on a load response. */ | ||
| @Value.Immutable | ||
| public interface Labels { | ||
| Labels EMPTY = ImmutableLabels.builder().build(); | ||
|
|
||
| /** Object-level labels. */ | ||
| Map<String, String> object(); | ||
|
|
||
| /** Field-level labels, keyed by field id. */ | ||
| List<FieldLabels> fields(); | ||
|
|
||
| /** Returns true when there are neither object-level nor field-level labels. */ | ||
| default boolean isEmpty() { | ||
| return object().isEmpty() && fields().isEmpty(); | ||
| } | ||
|
|
||
| /** Returns a shared empty instance. */ | ||
| static Labels empty() { | ||
| return EMPTY; | ||
| } | ||
| } | ||
81 changes: 81 additions & 0 deletions
81
core/src/main/java/org/apache/iceberg/rest/labels/LabelsParser.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,81 @@ | ||
| /* | ||
| * 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.rest.labels; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonGenerator; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import java.io.IOException; | ||
| import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
| import org.apache.iceberg.util.JsonUtil; | ||
|
|
||
| public class LabelsParser { | ||
| private static final String OBJECT = "object"; | ||
| private static final String FIELDS = "fields"; | ||
|
|
||
| private LabelsParser() {} | ||
|
|
||
| public static String toJson(Labels labels) { | ||
| return toJson(labels, false); | ||
| } | ||
|
|
||
| public static String toJson(Labels labels, boolean pretty) { | ||
| return JsonUtil.generate(gen -> toJson(labels, gen), pretty); | ||
| } | ||
|
|
||
| public static void toJson(Labels labels, JsonGenerator gen) throws IOException { | ||
| Preconditions.checkArgument(null != labels, "Invalid labels: null"); | ||
|
|
||
| gen.writeStartObject(); | ||
|
|
||
| if (!labels.object().isEmpty()) { | ||
| JsonUtil.writeStringMap(OBJECT, labels.object(), gen); | ||
| } | ||
|
|
||
| if (!labels.fields().isEmpty()) { | ||
| gen.writeArrayFieldStart(FIELDS); | ||
| for (FieldLabels fieldLabels : labels.fields()) { | ||
| FieldLabelsParser.toJson(fieldLabels, gen); | ||
| } | ||
|
|
||
| gen.writeEndArray(); | ||
| } | ||
|
|
||
| gen.writeEndObject(); | ||
| } | ||
|
|
||
| public static Labels fromJson(String json) { | ||
| return JsonUtil.parse(json, LabelsParser::fromJson); | ||
| } | ||
|
|
||
| public static Labels fromJson(JsonNode json) { | ||
| Preconditions.checkArgument(null != json, "Cannot parse labels from null object"); | ||
|
|
||
| ImmutableLabels.Builder builder = ImmutableLabels.builder(); | ||
|
|
||
| if (json.hasNonNull(OBJECT)) { | ||
| builder.object(JsonUtil.getStringMap(OBJECT, json)); | ||
| } | ||
|
|
||
| if (json.hasNonNull(FIELDS)) { | ||
| builder.fields(JsonUtil.getObjectList(FIELDS, json, FieldLabelsParser::fromJson)); | ||
| } | ||
|
|
||
| return builder.build(); | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
have we decided on the naming in the Spec for this? I still feel like
object()is really not telling me anything as a reader of the codeThere 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 discuss this here - #15750 (comment)
I'm thinking we are close to alignment, i don't have strong preferences honestly, just want to ensure it works :)