Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions core/src/main/java/org/apache/iceberg/rest/labels/FieldLabels.java
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");
}
}
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 core/src/main/java/org/apache/iceberg/rest/labels/Labels.java
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();

Copy link
Copy Markdown
Contributor

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 code

Copy link
Copy Markdown
Contributor Author

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 :)


/** 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;
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.rest.RESTResponse;
import org.apache.iceberg.rest.credentials.Credential;
import org.apache.iceberg.rest.labels.Labels;

/**
* A REST response that is used when a table is successfully loaded.
Expand All @@ -45,6 +46,7 @@ public class LoadTableResponse implements RESTResponse {
private Map<String, String> config;
private TableMetadata metadataWithLocation;
private List<Credential> credentials;
private Labels labels;

public LoadTableResponse() {
// Required for Jackson deserialization
Expand All @@ -54,11 +56,13 @@ private LoadTableResponse(
String metadataLocation,
TableMetadata metadata,
Map<String, String> config,
List<Credential> credentials) {
List<Credential> credentials,
Labels labels) {
this.metadataLocation = metadataLocation;
this.metadata = metadata;
this.config = config;
this.credentials = credentials;
this.labels = labels;
}

@Override
Expand Down Expand Up @@ -87,12 +91,18 @@ public List<Credential> credentials() {
return credentials != null ? credentials : ImmutableList.of();
}

/** Catalog-provided labels, or an empty instance when the response carries no labels. */
public Labels labels() {
return labels != null ? labels : Labels.empty();
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("metadataLocation", metadataLocation)
.add("metadata", metadata)
.add("config", config)
.add("labels", labels)
.toString();
}

Expand All @@ -105,6 +115,7 @@ public static class Builder {
private TableMetadata metadata;
private final Map<String, String> config = Maps.newHashMap();
private final List<Credential> credentials = Lists.newArrayList();
private Labels labels;

private Builder() {}

Expand Down Expand Up @@ -134,9 +145,14 @@ public Builder addAllCredentials(List<Credential> credentialsToAdd) {
return this;
}

public Builder withLabels(Labels tableLabels) {
this.labels = tableLabels;
return this;
}

public LoadTableResponse build() {
Preconditions.checkNotNull(metadata, "Invalid metadata: null");
return new LoadTableResponse(metadataLocation, metadata, config, credentials);
return new LoadTableResponse(metadataLocation, metadata, config, credentials, labels);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.rest.credentials.Credential;
import org.apache.iceberg.rest.credentials.CredentialParser;
import org.apache.iceberg.rest.labels.LabelsParser;
import org.apache.iceberg.util.JsonUtil;

public class LoadTableResponseParser {
Expand All @@ -34,6 +35,7 @@ public class LoadTableResponseParser {
private static final String METADATA = "metadata";
private static final String CONFIG = "config";
private static final String STORAGE_CREDENTIALS = "storage-credentials";
private static final String LABELS = "labels";

private LoadTableResponseParser() {}

Expand Down Expand Up @@ -70,6 +72,11 @@ public static void toJson(LoadTableResponse response, JsonGenerator gen) throws
gen.writeEndArray();
}

if (!response.labels().isEmpty()) {
gen.writeFieldName(LABELS);
LabelsParser.toJson(response.labels(), gen);
}

gen.writeEndObject();
}

Expand Down Expand Up @@ -101,6 +108,10 @@ public static LoadTableResponse fromJson(JsonNode json) {
builder.addAllCredentials(LoadCredentialsResponseParser.fromJson(json).credentials());
}

if (json.hasNonNull(LABELS)) {
builder.withLabels(LabelsParser.fromJson(JsonUtil.get(LABELS, json)));
}

return builder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import java.util.Map;
import org.apache.iceberg.rest.RESTResponse;
import org.apache.iceberg.rest.labels.Labels;
import org.apache.iceberg.view.ViewMetadata;
import org.immutables.value.Value;

Expand All @@ -31,6 +32,12 @@ public interface LoadViewResponse extends RESTResponse {

Map<String, String> config();

/** Catalog-provided labels, or an empty instance when the response carries no labels. */
@Value.Default
default Labels labels() {
return Labels.empty();
}

@Override
default void validate() {
// nothing to validate as it's not possible to create an invalid instance
Expand Down
Loading
Loading