Skip to content
Merged
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
5 changes: 4 additions & 1 deletion api/polaris-catalog-service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ val policyManagementModels =
"CatalogIdentifier",
"CreatePolicyRequest",
"LoadPolicyResponse",
"PolicyIdentifier",
"Policy",
"PolicyAttachmentTarget",
"AttachPolicyRequest",
Expand All @@ -59,6 +58,7 @@ dependencies {
implementation(libs.jakarta.inject.api)
implementation(libs.jakarta.validation.api)
implementation(libs.swagger.annotations)
implementation(libs.guava)

implementation(libs.jakarta.servlet.api)
implementation(libs.jakarta.ws.rs.api)
Expand Down Expand Up @@ -103,6 +103,9 @@ openApiGenerate {
"ErrorModel" to "org.apache.iceberg.rest.responses.ErrorResponse",
"IcebergErrorResponse" to "org.apache.iceberg.rest.responses.ErrorResponse",
"TableIdentifier" to "org.apache.iceberg.catalog.TableIdentifier",

// Custom types defined below
"PolicyIdentifier" to "org.apache.polaris.service.types.PolicyIdentifier",
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* 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.polaris.service.types;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import java.util.Objects;
import org.apache.iceberg.catalog.Namespace;

/**
* Represents a modified version of the PolicyIdentifier that is different from the one generated by
* the OpenAPI generator
*
* <p>the open api generation inlines the namespace definition, generates a {@code List<String>}
* directly, instead of generating a Namespace class. This version uses {@link
* org.apache.iceberg.catalog.Namespace} for namespace field.
*
* <p>TODO: make code generation use {@link org.apache.iceberg.catalog.Namespace} directly
*/
public class PolicyIdentifier {

private final Namespace namespace;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One way we could potentially fix the problem is to add a Namespace definition under schema that whose type is object, and then directly link to the iceberg namespace.

And in your policyId, link to this newly defined Namespace, during the generation, map the namespace to Iceberg namespace class. That could potentially fix the generation problem.

We can also try to do this in a follow up PR, but let's make sure we follow up on this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

basically,

    PolicyIdentifier:
      type: object
      required:
        - namespace
        - name
      properties:
        namespace:
          $ref: '#/components/schemas/Namespace'
        name:
          type: string
          nullable: false

Schema:
   Namespace:
       type: object
       $ref: '../iceberg-rest-catalog-open-api.yaml#/components/schemas/Namespace'

Then in the build in the importMappings add
"Namespace" to ""org.apache.iceberg.catalog.Namespace"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do it help? We will still need the Namespace class in Iceberg core lib, instead of the auto-gen class.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the ideal case is that we can auto-generate "PolicyIdentifier" class and let it use iceberg's "Namespace" class instead of a List<String>. I tried the

Namespace:
       $ref: '../iceberg-rest-catalog-open-api.yaml#/components/schemas/Namespace'

and unfortunately it does not work, we probably need to explore some open api generator configs to make auto-gen possible.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is wired, i believe it should work. can you get an issue and let's follow up on this?

private final String name;

/** Reference to one or more levels of a namespace */
@ApiModelProperty(
example = "[\"accounting\",\"tax\"]",
required = true,
value = "Reference to one or more levels of a namespace")
@JsonProperty(value = "namespace", required = true)
public Namespace getNamespace() {
return namespace;
}

/** */
@ApiModelProperty(required = true, value = "")
@JsonProperty(value = "name", required = true)
public String getName() {
return name;
}

@JsonCreator
public PolicyIdentifier(
@JsonProperty(value = "namespace", required = true) Namespace namespace,
@JsonProperty(value = "name", required = true) String name) {
this.namespace = Objects.requireNonNullElse(namespace, Namespace.empty());
this.name = name;
}

public static Builder builder() {
return new Builder();
}

public static Builder builder(Namespace namespace, String name) {
return new Builder(namespace, name);
}

public static final class Builder {
private Namespace namespace;
private String name;

private Builder() {}

private Builder(Namespace namespace, String name) {
this.namespace = Objects.requireNonNullElse(namespace, Namespace.empty());
this.name = name;
}

public Builder setNamespace(Namespace namespace) {
this.namespace = namespace;
return this;
}

public Builder setName(String name) {
this.name = name;
return this;
}

public PolicyIdentifier build() {
PolicyIdentifier inst = new PolicyIdentifier(namespace, name);
return inst;
}
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PolicyIdentifier policyIdentifier = (PolicyIdentifier) o;
return Objects.equals(this.namespace, policyIdentifier.namespace)
&& Objects.equals(this.name, policyIdentifier.name);
}

@Override
public int hashCode() {
return Objects.hash(namespace, name);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class PolicyIdentifier {\n");

sb.append(" namespace: ").append(toIndentedString(namespace)).append("\n");
sb.append(" name: ").append(toIndentedString(name)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces (except the first line).
*/
private String toIndentedString(Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ public class PolarisCatalogHelpers {
private PolarisCatalogHelpers() {}

public static List<String> tableIdentifierToList(TableIdentifier identifier) {
return identifierToList(identifier.namespace(), identifier.name());
}

public static List<String> identifierToList(Namespace namespace, String name) {
ImmutableList.Builder<String> fullList =
ImmutableList.builderWithExpectedSize(identifier.namespace().length() + 1);
fullList.addAll(Arrays.asList(identifier.namespace().levels()));
fullList.add(identifier.name());
ImmutableList.builderWithExpectedSize(namespace.length() + 1);
fullList.addAll(Arrays.asList(namespace.levels()));
fullList.add(name);
return fullList.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.polaris.core.policy.exceptions;

import org.apache.polaris.core.exceptions.PolarisException;

public class NoSuchPolicyException extends PolarisException {

public NoSuchPolicyException(String message) {
super(message);
}

public NoSuchPolicyException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.polaris.core.policy.exceptions;

import org.apache.polaris.core.exceptions.PolarisException;

public class PolicyVersionMismatchException extends PolarisException {
public PolicyVersionMismatchException(String message) {
super(message);
}

public PolicyVersionMismatchException(String message, Throwable cause) {
super(message, cause);
}
}
Loading