Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#13877] feat(api): Add Domain API (#13881)

Jerry Shao079b382ff8fd
diff --git a/api/src/main/java/org/apache/gravitino/MetadataObject.java b/api/src/main/java/org/apache/gravitino/MetadataObject.java
index 6dca45345d..c4804daf55 100644
--- a/api/src/main/java/org/apache/gravitino/MetadataObject.java
+++ b/api/src/main/java/org/apache/gravitino/MetadataObject.java
@@ -77,7 +77,13 @@ public interface MetadataObject {
/** A job template represents a reusable template for creating jobs in Gravitino. */
JOB_TEMPLATE,
/** A function represents a user-defined function registered in Gravitino. */
- FUNCTION;
+ FUNCTION,
+ /**
+ * A domain groups metadata objects from any catalog under a business name. A leaf object, such
+ * as a table, belongs to a domain when its effective tags, its own or those inherited from its
+ * schema and catalog, match the domain's backing tag. A domain cannot be tagged itself.
+ */
+ DOMAIN;
}
/**
diff --git a/api/src/main/java/org/apache/gravitino/MetadataObjects.java b/api/src/main/java/org/apache/gravitino/MetadataObjects.java
index 835c1636b3..67941d01e3 100644
--- a/api/src/main/java/org/apache/gravitino/MetadataObjects.java
+++ b/api/src/main/java/org/apache/gravitino/MetadataObjects.java
@@ -48,7 +48,8 @@ public class MetadataObjects {
MetadataObject.Type.TAG,
MetadataObject.Type.JOB,
MetadataObject.Type.JOB_TEMPLATE,
- MetadataObject.Type.POLICY);
+ MetadataObject.Type.POLICY,
+ MetadataObject.Type.DOMAIN);
private static final Set<MetadataObject.Type> VALID_TWO_LEVEL_NAME_TYPES =
Sets.newHashSet(MetadataObject.Type.SCHEMA);
diff --git a/api/src/main/java/org/apache/gravitino/domain/Domain.java b/api/src/main/java/org/apache/gravitino/domain/Domain.java
new file mode 100644
index 0000000000..d4201b2c54
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/Domain.java
@@ -0,0 +1,282 @@
+/*
+ * 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.gravitino.domain;
+
+import java.util.Map;
+import java.util.Optional;
+import javax.annotation.Nullable;
+import org.apache.gravitino.Auditable;
+import org.apache.gravitino.annotation.Evolving;
+import org.apache.gravitino.exceptions.BackingTagAlreadyBoundException;
+import org.apache.gravitino.exceptions.DomainAlreadyExistsException;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.gravitino.exceptions.NoSuchDomainException;
+import org.apache.gravitino.exceptions.NoSuchTagException;
+import org.apache.gravitino.exceptions.TagAlreadyExistsException;
+import org.apache.gravitino.tag.TagValue;
+
+/**
+ * A domain groups metadata objects from any catalog under a business name, such as "Finance". A
+ * leaf metadata object, of a type in {@link DomainMember#SUPPORTED_TYPES}, is a member of a domain
+ * when it carries the domain's backing tag, directly or inherited from its catalog or schema;
+ * catalogs and schemas are never members themselves. For the same tag, the assignment nearest to
+ * the object wins: a table that carries {@code dept=hr} under a schema that carries {@code
+ * dept=finance} is a member of the domain backed by {@code dept=hr} only.
+ *
+ * <p>A domain is either top-level or a sub-domain of exactly one top-level domain. Privileges on a
+ * domain, and its ownership, also apply to its sub-domains: the metalake, the current parent and
+ * the domain itself are checked together. An owner of any of them passes; otherwise a privilege
+ * holds when it is allowed on at least one of them and denied on none, so a deny on a sub-domain is
+ * not overridden by an allow on its parent.
+ *
+ * <p>A new domain is in {@link Status#DRAFT} status and, until it is published, is returned only to
+ * the users who can manage it.
+ *
+ * <p>A {@code Domain} is a snapshot. The operations on it that change the domain itself, such as
+ * {@link #publish()} and {@link #attachToParent(String)}, return a new snapshot and leave this one
+ * unchanged. They apply to the current state of the domain, not to the state this snapshot shows,
+ * and the last write wins. They address the domain by the name this snapshot shows: if the domain
+ * is renamed and a new domain takes its old name, they act on the new domain.
+ */
+@Evolving
+public interface Domain extends Auditable {
+
+ /** The status of a domain. */
+ enum Status {
+ /**
+ * The domain is returned only to the users who can manage it, including through its parent or
+ * the metalake.
+ */
+ DRAFT,
+ /** The domain is returned to all users who can use it. */
+ PUBLISHED
+ }
+
+ /**
+ * Returns the name of the domain.
+ *
+ * @return The name of the domain.
+ */
+ String name();
+
+ /**
+ * Returns the comment of the domain.
+ *
+ * @return The comment of the domain, or null if it is not set.
+ */
+ @Nullable
+ String comment();
+
+ /**
+ * Returns the properties of the domain.
+ *
+ * @return The properties of the domain.
+ */
+ Map<String, String> properties();
+
+ /**
+ * Returns the status of the domain.
+ *
+ * @return The status of the domain.
+ */
+ Status status();
+
+ /**
+ * Returns the name of the parent domain. The name is returned even when the parent is not visible
+ * to the caller, for example a draft parent; getting that parent still fails with {@link
+ * NoSuchDomainException}.
+ *
+ * @return The name of the parent domain, or empty if the domain is top-level.
+ */
+ Optional<String> parent();
+
+ /**
+ * Returns the backing tag of the domain. The metadata objects whose effective assignment of this
+ * tag matches it are the members of the domain. A backing tag without a value matches every
+ * assignment of the tag, with or without values. A backing tag with a value matches the
+ * assignments whose values contain it, compared exactly and case-sensitively.
+ *
+ * <p>The domain refers to the tag itself, not to its name: a renamed tag keeps backing the
+ * domain, and a new tag with the name of a deleted one does not.
+ *
+ * @return The backing tag of the domain, or empty if the backing tag has been removed or deleted
+ * and no new one has been set.
+ */
+ Optional<TagValue> backingTag();
+
+ /**
+ * Returns how the metadata object relates to this domain. It is set only on the domains returned
+ * by {@link SupportsDomains#listDomainsInfo()}, and lists every relation that applies, with no
+ * precedence among them.
+ *
+ * @return The relations of the metadata object to this domain, or an empty array if the domain is
+ * not listed for a metadata object.
+ */
+ DomainRelation[] relations();
+
+ /**
+ * Publishes the domain, making it visible to all users who can use it. A sub-domain can be
+ * published only when its parent is published. Publishing a published domain has no effect.
+ *
+ * @return The published domain.
+ * @throws NoSuchDomainException If the domain does not exist or is not visible to the caller.
+ * @throws IllegalArgumentException If the domain is a sub-domain and its parent is not published.
+ */
+ Domain publish() throws NoSuchDomainException, IllegalArgumentException;
+
+ /**
+ * Unpublishes the domain, making it visible only to the users who can manage it. A domain can be
+ * unpublished only when it has no published sub-domains. Unpublishing a draft domain has no
+ * effect.
+ *
+ * @return The unpublished domain.
+ * @throws NoSuchDomainException If the domain does not exist or is not visible to the caller.
+ * @throws IllegalArgumentException If the domain has published sub-domains.
+ */
+ Domain unpublish() throws NoSuchDomainException, IllegalArgumentException;
+
+ /**
+ * Attaches this domain as a sub-domain of the given parent. If this domain is a sub-domain of
+ * another domain, it is moved under the given parent. Attaching to the current parent has no
+ * effect. The backing tag is not renamed.
+ *
+ * <p>Since privileges on a parent, and its ownership, apply to its sub-domains, attaching or
+ * moving changes who can see and manage this domain without any grant changing: those of the old
+ * parent no longer apply to this domain, and those of the new parent do. A deny on this domain
+ * still applies.
+ *
+ * @param parent The name of the parent domain.
+ * @return The attached domain.
+ * @throws NoSuchDomainException If this domain or the parent does not exist or is not visible to
+ * the caller.
+ * @throws IllegalArgumentException If this domain cannot be attached to the parent, for example
+ * because the parent is this domain or a sub-domain, this domain has sub-domains, or this
+ * domain is published and the parent is not.
+ */
+ Domain attachToParent(String parent) throws NoSuchDomainException, IllegalArgumentException;
+
+ /**
+ * Detaches this domain from its current parent, making it a top-level domain, whatever parent
+ * this snapshot shows. Detaching a top-level domain has no effect. The backing tag is not
+ * renamed.
+ *
+ * <p>Since privileges on a parent, and its ownership, apply to its sub-domains, detaching changes
+ * who can see and manage this domain without any grant changing: those of the former parent no
+ * longer apply to this domain.
+ *
+ * @return The detached domain.
+ * @throws NoSuchDomainException If this domain does not exist or is not visible to the caller.
+ */
+ Domain detachFromParent() throws NoSuchDomainException;
+
+ /**
+ * Replaces the current backing tag of this domain, or removes it, whatever backing tag this
+ * snapshot shows. Members are not migrated: objects that carry only the old backing tag are no
+ * longer members of the domain. Setting the current backing tag has no effect.
+ *
+ * @param backingTag The existing tag, with an optional value, that backs the domain, or null to
+ * remove the backing tag.
+ * @return The updated domain.
+ * @throws NoSuchDomainException If this domain does not exist or is not visible to the caller.
+ * @throws NoSuchTagException If the tag does not exist.
+ * @throws BackingTagAlreadyBoundException If the binding conflicts with the backing tag of
+ * another domain.
+ * @throws ForbiddenException If a tag is set and the caller owns neither the tag nor the
+ * metalake. Removing the backing tag needs no privilege on the tag.
+ * @throws IllegalArgumentException If the value is not allowed by the tag.
+ */
+ Domain updateBackingTag(@Nullable TagValue backingTag)
+ throws NoSuchDomainException, NoSuchTagException, BackingTagAlreadyBoundException,
+ ForbiddenException, IllegalArgumentException;
+
+ /**
+ * Creates a sub-domain under this domain. The new sub-domain is in {@link Status#DRAFT} status.
+ *
+ * <p>The backing tag is always given explicitly. With {@code createBackingTag} set, a new tag
+ * without values is created with the given name and owned by the caller; otherwise the given
+ * existing tag, with its optional value, is bound. An existing tag is never adopted implicitly.
+ *
+ * <p>The sub-domain, a new backing tag and the link to this domain are created together: if the
+ * creation fails, none of them is created, and an existing tag to bind is left unchanged.
+ *
+ * @param name The name of the sub-domain.
+ * @param comment The comment of the sub-domain, or null.
+ * @param properties The properties of the sub-domain, or null.
+ * @param backingTag The backing tag of the sub-domain: the name of the tag to create, or an
+ * existing tag with an optional value.
+ * @param createBackingTag Whether to create the backing tag as a new tag.
+ * @return The created sub-domain.
+ * @throws NoSuchDomainException If this domain does not exist or is not visible to the caller.
+ * @throws DomainAlreadyExistsException If a domain with the same name already exists.
+ * @throws NoSuchTagException If the existing backing tag to bind does not exist.
+ * @throws TagAlreadyExistsException If the backing tag to create already exists.
+ * @throws BackingTagAlreadyBoundException If the binding conflicts with the backing tag of
+ * another domain.
+ * @throws ForbiddenException If the caller does not own the existing backing tag or the metalake,
+ * or may not create tags.
+ * @throws IllegalArgumentException If the backing tag is null, a value is given for a tag to
+ * create, the value is not allowed by the existing tag, this domain is itself a sub-domain,
+ * or the sub-domain cannot be created under this domain.
+ */
+ Domain createSubdomain(
+ String name,
+ @Nullable String comment,
+ @Nullable Map<String, String> properties,
+ TagValue backingTag,
+ boolean createBackingTag)
+ throws NoSuchDomainException, DomainAlreadyExistsException, NoSuchTagException,
+ TagAlreadyExistsException, BackingTagAlreadyBoundException, ForbiddenException,
+ IllegalArgumentException;
+
+ /**
+ * Lists the names of the sub-domains of this domain that are visible to the caller.
+ *
+ * @return The names of the sub-domains.
+ * @throws NoSuchDomainException If this domain does not exist or is not visible to the caller.
+ */
+ String[] listSubdomains() throws NoSuchDomainException;
+
+ /**
+ * Lists the sub-domains of this domain that are visible to the caller, with details.
+ *
+ * @return The sub-domains.
+ * @throws NoSuchDomainException If this domain does not exist or is not visible to the caller.
+ */
+ Domain[] listSubdomainsInfo() throws NoSuchDomainException;
+
+ /**
+ * Lists the members in the scope of this domain that match the filter. The scope of a top-level
+ * domain is its own members plus the members of its sub-domains visible to the caller; the scope
+ * of a sub-domain is its own members. Only objects of the {@link DomainMember#SUPPORTED_TYPES
+ * supported types} are members, and only the members the caller can access are returned.
+ *
+ * <p>A catalog or schema that carries a backing tag is expanded to the leaf objects under it that
+ * Gravitino has recorded, and the catalog or schema itself is not listed. Objects of an external
+ * catalog that Gravitino has not recorded yet are not listed. An object is listed once, with all
+ * its {@link DomainMember#relations() relations} to this domain. The members of this domain
+ * itself are the listed members whose relations include {@link DomainRelation.Type#DIRECT} or
+ * {@link DomainRelation.Type#INHERITED}.
+ *
+ * @param filter The filter of the members. {@code DomainMemberFilter.builder().build()} lists the
+ * whole scope of this domain.
+ * @return The members of this domain.
+ * @throws NoSuchDomainException If this domain does not exist or is not visible to the caller.
+ */
+ DomainMember[] listMembers(DomainMemberFilter filter) throws NoSuchDomainException;
+}
diff --git a/api/src/main/java/org/apache/gravitino/domain/DomainChange.java b/api/src/main/java/org/apache/gravitino/domain/DomainChange.java
new file mode 100644
index 0000000000..caefbbcbdf
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/DomainChange.java
@@ -0,0 +1,310 @@
+/*
+ * 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.gravitino.domain;
+
+import java.util.Objects;
+import javax.annotation.Nullable;
+import org.apache.gravitino.annotation.Evolving;
+
+/**
+ * Interface for supporting domain changes. This interface will be used to provide domain
+ * modification operations for each domain implementation.
+ */
+@Evolving
+public interface DomainChange {
+
+ /**
+ * Creates a new domain change to rename the domain. The backing tag of the domain is not renamed.
+ *
+ * @param newName The new name of the domain.
+ * @return The domain change.
+ */
+ static DomainChange rename(String newName) {
+ return new RenameDomain(newName);
+ }
+
+ /**
+ * Creates a new domain change to update the domain comment.
+ *
+ * @param newComment The new comment for the domain, or null to remove the comment.
+ * @return The domain change.
+ */
+ static DomainChange updateComment(@Nullable String newComment) {
+ return new UpdateDomainComment(newComment);
+ }
+
+ /**
+ * Creates a new domain change to set the property and value for the domain.
+ *
+ * @param property The property name to set.
+ * @param value The value to set the property to.
+ * @return The domain change.
+ */
+ static DomainChange setProperty(String property, String value) {
+ return new SetProperty(property, value);
+ }
+
+ /**
+ * Creates a new domain change to remove a property from the domain.
+ *
+ * @param property The property name to remove.
+ * @return The domain change.
+ */
+ static DomainChange removeProperty(String property) {
+ return new RemoveProperty(property);
+ }
+
+ /** A domain change to rename the domain. */
+ final class RenameDomain implements DomainChange {
+ private final String newName;
+
+ private RenameDomain(String newName) {
+ this.newName = newName;
+ }
+
+ /**
+ * Get the new name of the domain.
+ *
+ * @return The new name of the domain.
+ */
+ public String getNewName() {
+ return newName;
+ }
+
+ /**
+ * Compares this RenameDomain instance with another object for equality.
+ *
+ * @param o The object to compare with this instance.
+ * @return true if the given object represents the same domain rename operation; false
+ * otherwise.
+ */
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ RenameDomain that = (RenameDomain) o;
+ return Objects.equals(newName, that.newName);
+ }
+
+ /**
+ * Generates a hash code for this RenameDomain instance.
+ *
+ * @return A hash code value for this domain rename operation.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(newName);
+ }
+
+ /**
+ * Returns a string representation of the RenameDomain instance.
+ *
+ * @return A string representation of the RenameDomain instance.
+ */
+ @Override
+ public String toString() {
+ return "RENAMEDOMAIN " + newName;
+ }
+ }
+
+ /** A domain change to update the domain comment. */
+ final class UpdateDomainComment implements DomainChange {
+ @Nullable private final String newComment;
+
+ private UpdateDomainComment(@Nullable String newComment) {
+ this.newComment = newComment;
+ }
+
+ /**
+ * Get the new comment of the domain.
+ *
+ * @return The new comment of the domain, or null to remove the comment.
+ */
+ @Nullable
+ public String getNewComment() {
+ return newComment;
+ }
+
+ /**
+ * Compares this UpdateDomainComment instance with another object for equality.
+ *
+ * @param o The object to compare with this instance.
+ * @return true if the given object represents the same domain comment update operation; false
+ * otherwise.
+ */
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ UpdateDomainComment that = (UpdateDomainComment) o;
+ return Objects.equals(newComment, that.newComment);
+ }
+
+ /**
+ * Generates a hash code for this UpdateDomainComment instance.
+ *
+ * @return A hash code value for this domain comment update operation.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(newComment);
+ }
+
+ /**
+ * Returns a string representation of the UpdateDomainComment instance.
+ *
+ * @return A string representation of the UpdateDomainComment instance.
+ */
+ @Override
+ public String toString() {
+ return "UPDATEDOMAINCOMMENT " + newComment;
+ }
+ }
+
+ /** A domain change to set the property and value for the domain. */
+ final class SetProperty implements DomainChange {
+ private final String property;
+ private final String value;
+
+ private SetProperty(String property, String value) {
+ this.property = property;
+ this.value = value;
+ }
+
+ /**
+ * Get the property name to set.
+ *
+ * @return The property name to set.
+ */
+ public String getProperty() {
+ return property;
+ }
+
+ /**
+ * Get the value to set the property to.
+ *
+ * @return The value to set the property to.
+ */
+ public String getValue() {
+ return value;
+ }
+
+ /**
+ * Compares this SetProperty instance with another object for equality.
+ *
+ * @param o The object to compare with this instance.
+ * @return true if the given object represents the same property setting; false otherwise.
+ */
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ SetProperty that = (SetProperty) o;
+ return Objects.equals(property, that.property) && Objects.equals(value, that.value);
+ }
+
+ /**
+ * Generates a hash code for this SetProperty instance.
+ *
+ * @return A hash code value for this property setting.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(property, value);
+ }
+
+ /**
+ * Returns a string representation of the SetProperty instance.
+ *
+ * @return A string representation of the SetProperty instance.
+ */
+ @Override
+ public String toString() {
+ return "SETDOMAINPROPERTY " + property + " = " + value;
+ }
+ }
+
+ /** A domain change to remove a property from the domain. */
+ final class RemoveProperty implements DomainChange {
+ private final String property;
+
+ private RemoveProperty(String property) {
+ this.property = property;
+ }
+
+ /**
+ * Get the property name to remove.
+ *
+ * @return The property name to remove.
+ */
+ public String getProperty() {
+ return property;
+ }
+
+ /**
+ * Compares this RemoveProperty instance with another object for equality.
+ *
+ * @param o The object to compare with this instance.
+ * @return true if the given object represents the same property removal; false otherwise.
+ */
+ @Override
+ public boolean equals(@Nullable Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ RemoveProperty that = (RemoveProperty) o;
+ return Objects.equals(property, that.property);
+ }
+
+ /**
+ * Generates a hash code for this RemoveProperty instance.
+ *
+ * @return A hash code value for this property removal.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hashCode(property);
+ }
+
+ /**
+ * Returns a string representation of the RemoveProperty instance.
+ *
+ * @return A string representation of the RemoveProperty instance.
+ */
+ @Override
+ public String toString() {
+ return "REMOVEDOMAINPROPERTY " + property;
+ }
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/domain/DomainMember.java b/api/src/main/java/org/apache/gravitino/domain/DomainMember.java
new file mode 100644
index 0000000000..d040acf9b0
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/DomainMember.java
@@ -0,0 +1,65 @@
+/*
+ * 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.gravitino.domain;
+
+import com.google.common.collect.Sets;
+import java.util.Set;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.annotation.Evolving;
+
+/**
+ * A metadata object that is a member of a domain. Only leaf objects, of the {@link #SUPPORTED_TYPES
+ * supported types}, are members. Catalogs and schemas are never members: a backing tag on a catalog
+ * or schema makes the leaf objects under it members through tag inheritance.
+ */
+@Evolving
+public interface DomainMember {
+
+ /** The metadata object types that can be members of a domain, all of them leaf objects. */
+ Set<MetadataObject.Type> SUPPORTED_TYPES =
+ Sets.immutableEnumSet(
+ MetadataObject.Type.TABLE,
+ MetadataObject.Type.VIEW,
+ MetadataObject.Type.FILESET,
+ MetadataObject.Type.TOPIC,
+ MetadataObject.Type.MODEL,
+ MetadataObject.Type.SEMANTIC_MODEL);
+
+ /**
+ * Returns the metadata object that is a member of the domain.
+ *
+ * @return The metadata object.
+ */
+ MetadataObject object();
+
+ /**
+ * Returns how the member relates to the listed domain. The {@link DomainMemberFilter} selects the
+ * members only: every relation of a listed member to the listed domain is reported, with no
+ * precedence among them, whatever domains the filter selects. For example, when schema {@code
+ * cat.db} carries the backing tag of "Finance" and table {@code cat.db.trades} carries that of
+ * its sub-domain "Trading", listing Finance reports the table as {@link
+ * DomainRelation.Type#INHERITED} from {@code cat.db} and {@link
+ * DomainRelation.Type#VIA_SUBDOMAIN} Trading. The member belongs to the listed domain itself when
+ * its relations include {@link DomainRelation.Type#DIRECT} or {@link
+ * DomainRelation.Type#INHERITED}.
+ *
+ * @return The relations of the member to the listed domain, never empty.
+ */
+ DomainRelation[] relations();
+}
diff --git a/api/src/main/java/org/apache/gravitino/domain/DomainMemberFilter.java b/api/src/main/java/org/apache/gravitino/domain/DomainMemberFilter.java
new file mode 100644
index 0000000000..e852a7d1f9
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/DomainMemberFilter.java
@@ -0,0 +1,227 @@
+/*
+ * 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.gravitino.domain;
+
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableSet;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.Set;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.annotation.Evolving;
+
+/**
+ * The filter of {@link Domain#listMembers(DomainMemberFilter)}. Each option narrows the listed
+ * members, and an option that is not set does not narrow them, so a filter built without setting
+ * any option lists the whole scope of the domain.
+ *
+ * <p>The scope of a top-level domain is its own members plus the members of its sub-domains visible
+ * to the caller; the scope of a sub-domain is its own members. The domains option selects the
+ * scopes to list: each name is the listed domain itself or one of its sub-domains, and the members
+ * in the union of their scopes are listed. For example, on domain "Finance" with sub-domains "Tax"
+ * and "Trading":
+ *
+ * <ul>
+ * <li>not set, or {@code withDomains("Finance")}: the members of Finance, Tax and Trading;
+ * <li>{@code withDomains("Tax")}: only the members of Tax;
+ * <li>{@code withDomains("Tax", "Trading")}: the members of Tax and Trading.
+ * </ul>
+ *
+ * <p>The members of the listed domain itself are the listed members whose {@link
+ * DomainMember#relations() relations} include {@link DomainRelation.Type#DIRECT} or {@link
+ * DomainRelation.Type#INHERITED}; select them to keep only those.
+ */
+@Evolving
+public final class DomainMemberFilter {
+
+ private final Set<MetadataObject.Type> types;
+ @Nullable private final String nameContains;
+ private final Set<String> domains;
+
+ private DomainMemberFilter(Builder builder) {
+ this.types = builder.types;
+ this.nameContains = builder.nameContains;
+ this.domains = builder.domains;
+ }
+
+ /**
+ * Creates a builder for a domain member filter.
+ *
+ * @return A new builder.
+ */
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ /**
+ * Returns the metadata object types of the members to list.
+ *
+ * @return The metadata object types, or an empty set to list members of all {@link
+ * DomainMember#SUPPORTED_TYPES supported types}.
+ */
+ public Set<MetadataObject.Type> types() {
+ return types;
+ }
+
+ /**
+ * Returns the text the full name of a member must contain, ignoring case.
+ *
+ * @return The text, or empty to match any name.
+ */
+ public Optional<String> nameContains() {
+ return Optional.ofNullable(nameContains);
+ }
+
+ /**
+ * Returns the domains whose scopes are listed: the listed domain itself, its sub-domains, or
+ * both.
+ *
+ * @return The names of the domains, or an empty set to list the whole scope of the listed domain.
+ */
+ public Set<String> domains() {
+ return domains;
+ }
+
+ /**
+ * Compares this filter with another object.
+ *
+ * @param other The object to compare.
+ * @return {@code true} if the object is a filter with the same fields.
+ */
+ @Override
+ public boolean equals(@Nullable Object other) {
+ if (this == other) {
+ return true;
+ }
+ if (!(other instanceof DomainMemberFilter)) {
+ return false;
+ }
+ DomainMemberFilter that = (DomainMemberFilter) other;
+ return types.equals(that.types)
+ && Objects.equals(nameContains, that.nameContains)
+ && domains.equals(that.domains);
+ }
+
+ /**
+ * Returns the hash code of this filter.
+ *
+ * @return The hash code.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(types, nameContains, domains);
+ }
+
+ /**
+ * Returns a string representation of this filter.
+ *
+ * @return The string representation.
+ */
+ @Override
+ public String toString() {
+ return "DomainMemberFilter{"
+ + "types="
+ + types
+ + ", nameContains="
+ + nameContains
+ + ", domains="
+ + domains
+ + "}";
+ }
+
+ /** A builder for {@link DomainMemberFilter}. */
+ public static final class Builder {
+
+ private Set<MetadataObject.Type> types = ImmutableSet.of();
+ @Nullable private String nameContains;
+ private Set<String> domains = ImmutableSet.of();
+
+ private Builder() {}
+
+ /**
+ * Sets the metadata object types of the members to list. Defaults to all {@link
+ * DomainMember#SUPPORTED_TYPES supported types}.
+ *
+ * @param types The metadata object types, or none to list members of all supported types.
+ * @return This builder.
+ * @throws IllegalArgumentException If the types are null, contain null, or contain a type that
+ * is not in {@link DomainMember#SUPPORTED_TYPES}.
+ */
+ public Builder withTypes(MetadataObject.Type... types) {
+ Preconditions.checkArgument(types != null, "types must not be null");
+ for (MetadataObject.Type type : types) {
+ Preconditions.checkArgument(type != null, "types must not contain null");
+ Preconditions.checkArgument(
+ DomainMember.SUPPORTED_TYPES.contains(type),
+ "Type %s cannot be a domain member, supported types are %s",
+ type,
+ DomainMember.SUPPORTED_TYPES);
+ }
+ this.types = ImmutableSet.copyOf(types);
+ return this;
+ }
+
+ /**
+ * Sets the text the full name of a member must contain, ignoring case.
+ *
+ * @param nameContains The text, or null to match any name.
+ * @return This builder.
+ * @throws IllegalArgumentException If the text is blank.
+ */
+ public Builder withNameContains(@Nullable String nameContains) {
+ Preconditions.checkArgument(
+ nameContains == null || StringUtils.isNotBlank(nameContains),
+ "nameContains must not be blank");
+ this.nameContains = nameContains;
+ return this;
+ }
+
+ /**
+ * Sets the domains whose scopes are listed. Each name is the listed domain itself, whose scope
+ * includes its visible sub-domains, or one of its sub-domains. Names that are neither, or that
+ * are not visible to the caller, are ignored when the members are listed; if all names are
+ * ignored, no member is listed. Defaults to the whole scope of the listed domain.
+ *
+ * @param domains The names of the domains, or none to list the whole scope of the listed
+ * domain.
+ * @return This builder.
+ * @throws IllegalArgumentException If the names are null, or contain a null or blank name.
+ */
+ public Builder withDomains(String... domains) {
+ Preconditions.checkArgument(domains != null, "domains must not be null");
+ for (String domain : domains) {
+ Preconditions.checkArgument(
+ StringUtils.isNotBlank(domain), "domains must not contain a blank name");
+ }
+ this.domains = ImmutableSet.copyOf(domains);
+ return this;
+ }
+
+ /**
+ * Builds a domain member filter.
+ *
+ * @return The immutable domain member filter.
+ */
+ public DomainMemberFilter build() {
+ return new DomainMemberFilter(this);
+ }
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/domain/DomainOperations.java b/api/src/main/java/org/apache/gravitino/domain/DomainOperations.java
new file mode 100644
index 0000000000..3992847edb
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/DomainOperations.java
@@ -0,0 +1,129 @@
+/*
+ * 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.gravitino.domain;
+
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.gravitino.annotation.Evolving;
+import org.apache.gravitino.exceptions.BackingTagAlreadyBoundException;
+import org.apache.gravitino.exceptions.DomainAlreadyExistsException;
+import org.apache.gravitino.exceptions.ForbiddenException;
+import org.apache.gravitino.exceptions.NoSuchDomainException;
+import org.apache.gravitino.exceptions.NoSuchMetalakeException;
+import org.apache.gravitino.exceptions.NoSuchTagException;
+import org.apache.gravitino.exceptions.NonEmptyDomainException;
+import org.apache.gravitino.exceptions.TagAlreadyExistsException;
+import org.apache.gravitino.tag.TagValue;
+
+/**
+ * Interface for the domain operations of a metalake. Operations on a single domain, such as
+ * publishing it or managing its sub-domains, are on {@link Domain}.
+ *
+ * <p>A domain in {@link Domain.Status#DRAFT} status is returned only to the users who can manage
+ * it, including through its parent or the metalake (see {@link Domain}). For other users it is
+ * omitted from lists, and getting it throws {@link NoSuchDomainException}.
+ */
+@Evolving
+public interface DomainOperations {
+
+ /**
+ * Lists the names of the domains in the metalake, including sub-domains.
+ *
+ * @return The names of the domains.
+ * @throws NoSuchMetalakeException If the metalake does not exist.
+ */
+ String[] listDomains() throws NoSuchMetalakeException;
+
+ /**
+ * Lists the domains in the metalake with details, including sub-domains.
+ *
+ * @return The domains.
+ * @throws NoSuchMetalakeException If the metalake does not exist.
+ */
+ Domain[] listDomainsInfo() throws NoSuchMetalakeException;
+
+ /**
+ * Gets a domain by its name.
+ *
+ * @param name The name of the domain.
+ * @return The domain.
+ * @throws NoSuchDomainException If the domain does not exist or is not visible to the caller.
+ */
+ Domain getDomain(String name) throws NoSuchDomainException;
+
+ /**
+ * Creates a top-level domain in {@link Domain.Status#DRAFT} status. Use {@link
+ * Domain#createSubdomain(String, String, Map, TagValue, boolean)} to create a sub-domain.
+ *
+ * <p>The backing tag is always given explicitly. With {@code createBackingTag} set, a new tag
+ * without values is created with the given name and owned by the caller; otherwise the given
+ * existing tag, with its optional value, is bound. An existing tag is never adopted implicitly.
+ *
+ * <p>The domain and a new backing tag are created together: if the creation fails, neither is
+ * created, and an existing tag to bind is left unchanged.
+ *
+ * @param name The name of the domain.
+ * @param comment The comment of the domain, or null.
+ * @param properties The properties of the domain, or null.
+ * @param backingTag The backing tag of the domain: the name of the tag to create, or an existing
+ * tag with an optional value.
+ * @param createBackingTag Whether to create the backing tag as a new tag.
+ * @return The created domain.
+ * @throws DomainAlreadyExistsException If a domain with the same name already exists.
+ * @throws NoSuchTagException If the existing backing tag to bind does not exist.
+ * @throws TagAlreadyExistsException If the backing tag to create already exists.
+ * @throws BackingTagAlreadyBoundException If the binding conflicts with the backing tag of
+ * another domain.
+ * @throws ForbiddenException If the caller does not own the existing backing tag or the metalake,
+ * or may not create tags.
+ * @throws IllegalArgumentException If the backing tag is null, a value is given for a tag to
+ * create, or the value is not allowed by the existing tag.
+ */
+ Domain createDomain(
+ String name,
+ @Nullable String comment,
+ @Nullable Map<String, String> properties,
+ TagValue backingTag,
+ boolean createBackingTag)
+ throws DomainAlreadyExistsException, NoSuchTagException, TagAlreadyExistsException,
+ BackingTagAlreadyBoundException, ForbiddenException, IllegalArgumentException;
+
+ /**
+ * Alters a domain. Renaming a domain does not rename its backing tag.
+ *
+ * @param name The name of the domain.
+ * @param changes The changes to apply to the domain.
+ * @return The altered domain.
+ * @throws NoSuchDomainException If the domain does not exist or is not visible to the caller.
+ * @throws DomainAlreadyExistsException If the domain is renamed and a domain with the new name
+ * already exists.
+ * @throws IllegalArgumentException If the changes cannot be applied to the domain.
+ */
+ Domain alterDomain(String name, DomainChange... changes)
+ throws NoSuchDomainException, DomainAlreadyExistsException, IllegalArgumentException;
+
+ /**
+ * Deletes a domain. The backing tag and the tags on the member objects are kept.
+ *
+ * @param name The name of the domain.
+ * @return True if the domain is deleted, false if it does not exist.
+ * @throws NonEmptyDomainException If the domain has sub-domains.
+ */
+ boolean deleteDomain(String name) throws NonEmptyDomainException;
+}
diff --git a/api/src/main/java/org/apache/gravitino/domain/DomainRelation.java b/api/src/main/java/org/apache/gravitino/domain/DomainRelation.java
new file mode 100644
index 0000000000..f1a2041f28
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/DomainRelation.java
@@ -0,0 +1,155 @@
+/*
+ * 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.gravitino.domain;
+
+import com.google.common.base.Preconditions;
+import java.util.Objects;
+import java.util.Optional;
+import javax.annotation.Nullable;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.MetadataObjects;
+import org.apache.gravitino.annotation.Evolving;
+
+/**
+ * How a leaf metadata object relates to a domain, always relative to the listed domain. An object
+ * can relate to a domain in several ways at once, and every relation that applies is reported, with
+ * no precedence among them. For example, a table whose schema carries the backing tag of "Finance"
+ * and which itself carries that of its sub-domain "Trading" relates to Finance as {@link
+ * Type#INHERITED} from the schema and as {@link Type#VIA_SUBDOMAIN} Trading.
+ */
+@Evolving
+public final class DomainRelation {
+
+ /** The kind of a relation between a metadata object and a domain. */
+ public enum Type {
+ /** The object itself carries the domain's backing tag. */
+ DIRECT,
+ /** The object inherits the domain's backing tag from its catalog or schema. */
+ INHERITED,
+ /** A sub-domain of the domain, visible to the caller, has the object in its scope. */
+ VIA_SUBDOMAIN
+ }
+
+ private final Type type;
+ @Nullable private final MetadataObject source;
+
+ private DomainRelation(Type type, @Nullable MetadataObject source) {
+ this.type = type;
+ this.source = source;
+ }
+
+ /**
+ * Creates a relation for an object that carries the domain's backing tag itself.
+ *
+ * @return The relation.
+ */
+ public static DomainRelation direct() {
+ return new DomainRelation(Type.DIRECT, null);
+ }
+
+ /**
+ * Creates a relation for an object that inherits the domain's backing tag.
+ *
+ * @param source The nearest catalog or schema that carries the backing tag. Only its name and
+ * type are kept.
+ * @return The relation.
+ * @throws IllegalArgumentException If the source is null, or is neither a catalog nor a schema.
+ */
+ public static DomainRelation inherited(MetadataObject source) {
+ Preconditions.checkArgument(source != null, "source must not be null");
+ Preconditions.checkArgument(
+ source.type() == MetadataObject.Type.CATALOG || source.type() == MetadataObject.Type.SCHEMA,
+ "An inherited relation must come from a catalog or schema, but got %s",
+ source.type());
+ return new DomainRelation(
+ Type.INHERITED, MetadataObjects.parse(source.fullName(), source.type()));
+ }
+
+ /**
+ * Creates a relation for an object in the scope of a sub-domain of the domain.
+ *
+ * @param subdomain The name of the sub-domain.
+ * @return The relation, whose source is the sub-domain as a {@link MetadataObject.Type#DOMAIN}
+ * metadata object.
+ * @throws IllegalArgumentException If the name is blank.
+ */
+ public static DomainRelation viaSubdomain(String subdomain) {
+ Preconditions.checkArgument(StringUtils.isNotBlank(subdomain), "subdomain must not be blank");
+ return new DomainRelation(
+ Type.VIA_SUBDOMAIN, MetadataObjects.of(null, subdomain, MetadataObject.Type.DOMAIN));
+ }
+
+ /**
+ * Returns the kind of the relation.
+ *
+ * @return The kind of the relation.
+ */
+ public Type type() {
+ return type;
+ }
+
+ /**
+ * Returns the source of the relation: the catalog or schema the backing tag is inherited from for
+ * {@link Type#INHERITED}, or the sub-domain for {@link Type#VIA_SUBDOMAIN}.
+ *
+ * @return The source of the relation, or empty for {@link Type#DIRECT}.
+ */
+ public Optional<MetadataObject> source() {
+ return Optional.ofNullable(source);
+ }
+
+ /**
+ * Compares this relation with another object.
+ *
+ * @param other The object to compare.
+ * @return {@code true} if the object is a relation of the same kind and source.
+ */
+ @Override
+ public boolean equals(@Nullable Object other) {
+ if (this == other) {
+ return true;
+ }
+ if (!(other instanceof DomainRelation)) {
+ return false;
+ }
+ DomainRelation that = (DomainRelation) other;
+ return type == that.type && Objects.equals(source, that.source);
+ }
+
+ /**
+ * Returns the hash code of this relation.
+ *
+ * @return The hash code.
+ */
+ @Override
+ public int hashCode() {
+ return Objects.hash(type, source);
+ }
+
+ /**
+ * Returns a string representation of this relation.
+ *
+ * @return The string representation.
+ */
+ @Override
+ public String toString() {
+ return source == null ? type.name() : type.name() + " " + source.fullName();
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/domain/SupportsDomains.java b/api/src/main/java/org/apache/gravitino/domain/SupportsDomains.java
new file mode 100644
index 0000000000..11e2ed32fe
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/domain/SupportsDomains.java
@@ -0,0 +1,61 @@
+/*
+ * 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.gravitino.domain;
+
+import org.apache.gravitino.annotation.Evolving;
+
+/**
+ * Interface for listing the domains of a leaf metadata object, such as a table, view or fileset.
+ * Catalogs and schemas do not support it, since they are never members of a domain; the domain a
+ * catalog or schema passes down to the objects under it is visible from its tags.
+ *
+ * <p>The domains listed are all the domains visible to the caller whose scope contains the object:
+ * the domains whose backing tag the object's effective tags match, and the parents of those that
+ * are sub-domains visible to the caller. The effective tags are the object's own tags plus those
+ * inherited from its schema and catalog; for the same tag, the assignment nearest to the object
+ * wins. Each domain comes with its {@link Domain#relations() relations} to the object; an object
+ * tagged only with the backing tag of sub-domain "Trading" lists Trading as {@link
+ * DomainRelation.Type#DIRECT} and its parent "Finance" as {@link DomainRelation.Type#VIA_SUBDOMAIN}
+ * Trading.
+ *
+ * <p>To add an object to a domain or remove it, associate the domain's backing tag with the object
+ * or remove it, through {@link org.apache.gravitino.tag.SupportsTags}. That changes only the tags
+ * assigned to the object itself, and the domains are then derived again from its effective tags. An
+ * object that inherits the backing tag from its schema or catalog stays in the domain until the tag
+ * is removed there, and removing a tag from the object can make an inherited assignment of the same
+ * tag take effect again.
+ */
+@Evolving
+public interface SupportsDomains {
+
+ /**
+ * Lists the names of the domains whose scope contains the object.
+ *
+ * @return The names of the domains.
+ */
+ String[] listDomains();
+
+ /**
+ * Lists the domains whose scope contains the object, with details. {@link Domain#relations()} of
+ * each domain returns how the object relates to it.
+ *
+ * @return The domains.
+ */
+ Domain[] listDomainsInfo();
+}
diff --git a/api/src/main/java/org/apache/gravitino/exceptions/BackingTagAlreadyBoundException.java b/api/src/main/java/org/apache/gravitino/exceptions/BackingTagAlreadyBoundException.java
new file mode 100644
index 0000000000..bca65fd3d8
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/exceptions/BackingTagAlreadyBoundException.java
@@ -0,0 +1,53 @@
+/*
+ * 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.gravitino.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+
+/**
+ * Exception thrown when a backing tag binding conflicts with the binding of another domain: a tag
+ * has either one binding without a value, or bindings with values where each value backs at most
+ * one domain.
+ */
+public class BackingTagAlreadyBoundException extends AlreadyExistsException {
+
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public BackingTagAlreadyBoundException(String message, Object... args) {
+ super(message, args);
+ }
+
+ /**
+ * Constructs a new exception with the specified detail message and cause.
+ *
+ * @param cause the cause.
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public BackingTagAlreadyBoundException(Throwable cause, String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/exceptions/DomainAlreadyExistsException.java b/api/src/main/java/org/apache/gravitino/exceptions/DomainAlreadyExistsException.java
new file mode 100644
index 0000000000..9d030ae069
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/exceptions/DomainAlreadyExistsException.java
@@ -0,0 +1,49 @@
+/*
+ * 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.gravitino.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+
+/** Exception thrown when a domain with specified name already exists. */
+public class DomainAlreadyExistsException extends AlreadyExistsException {
+
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public DomainAlreadyExistsException(String message, Object... args) {
+ super(message, args);
+ }
+
+ /**
+ * Constructs a new exception with the specified detail message and cause.
+ *
+ * @param cause the cause.
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public DomainAlreadyExistsException(Throwable cause, String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/exceptions/NoSuchDomainException.java b/api/src/main/java/org/apache/gravitino/exceptions/NoSuchDomainException.java
new file mode 100644
index 0000000000..847d6977ef
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/exceptions/NoSuchDomainException.java
@@ -0,0 +1,49 @@
+/*
+ * 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.gravitino.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+
+/** Exception thrown when a domain with specified name does not exist. */
+public class NoSuchDomainException extends NotFoundException {
+
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public NoSuchDomainException(String message, Object... args) {
+ super(message, args);
+ }
+
+ /**
+ * Constructs a new exception with the specified detail message and cause.
+ *
+ * @param cause the cause.
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public NoSuchDomainException(Throwable cause, String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/exceptions/NonEmptyDomainException.java b/api/src/main/java/org/apache/gravitino/exceptions/NonEmptyDomainException.java
new file mode 100644
index 0000000000..ca772cf1c4
--- /dev/null
+++ b/api/src/main/java/org/apache/gravitino/exceptions/NonEmptyDomainException.java
@@ -0,0 +1,48 @@
+/*
+ * 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.gravitino.exceptions;
+
+import com.google.errorprone.annotations.FormatMethod;
+import com.google.errorprone.annotations.FormatString;
+
+/** Exception thrown when a domain is not empty, for example when it has sub-domains. */
+public class NonEmptyDomainException extends GravitinoRuntimeException {
+ /**
+ * Constructs a new exception with the specified detail message.
+ *
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public NonEmptyDomainException(@FormatString String message, Object... args) {
+ super(message, args);
+ }
+
+ /**
+ * Constructs a new exception with the specified detail message and cause.
+ *
+ * @param cause the cause.
+ * @param message the detail message.
+ * @param args the arguments to the message.
+ */
+ @FormatMethod
+ public NonEmptyDomainException(Throwable cause, @FormatString String message, Object... args) {
+ super(cause, message, args);
+ }
+}
diff --git a/api/src/main/java/org/apache/gravitino/file/Fileset.java b/api/src/main/java/org/apache/gravitino/file/Fileset.java
index 36577de3ed..240652948f 100644
--- a/api/src/main/java/org/apache/gravitino/file/Fileset.java
+++ b/api/src/main/java/org/apache/gravitino/file/Fileset.java
@@ -26,6 +26,7 @@ import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.Evolving;
import org.apache.gravitino.authorization.SupportsRoles;
import org.apache.gravitino.credential.SupportsCredentials;
+import org.apache.gravitino.domain.SupportsDomains;
import org.apache.gravitino.policy.SupportsPolicies;
import org.apache.gravitino.secret.SupportsSecrets;
import org.apache.gravitino.tag.SupportsTags;
@@ -257,6 +258,16 @@ public interface Fileset extends Auditable {
throw new UnsupportedOperationException("Fileset does not support tag operations.");
}
+ /**
+ * Returns the domain operations for this fileset.
+ *
+ * @return The {@link SupportsDomains} if the fileset supports domain operations.
+ * @throws UnsupportedOperationException If the fileset does not support domain operations.
+ */
+ default SupportsDomains supportsDomains() {
+ throw new UnsupportedOperationException("Fileset does not support domain operations.");
+ }
+
/**
* @return The {@link SupportsPolicies} if the fileset supports policy operations.
* @throws UnsupportedOperationException If the fileset does not support policy operations.
diff --git a/api/src/main/java/org/apache/gravitino/messaging/Topic.java b/api/src/main/java/org/apache/gravitino/messaging/Topic.java
index a386d13d63..f5657121f8 100644
--- a/api/src/main/java/org/apache/gravitino/messaging/Topic.java
+++ b/api/src/main/java/org/apache/gravitino/messaging/Topic.java
@@ -25,6 +25,7 @@ import org.apache.gravitino.Auditable;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.Evolving;
import org.apache.gravitino.authorization.SupportsRoles;
+import org.apache.gravitino.domain.SupportsDomains;
import org.apache.gravitino.policy.SupportsPolicies;
import org.apache.gravitino.secret.SupportsSecrets;
import org.apache.gravitino.tag.SupportsTags;
@@ -68,6 +69,16 @@ public interface Topic extends Auditable {
throw new UnsupportedOperationException("Topic does not support tag operations.");
}
+ /**
+ * Returns the domain operations for this topic.
+ *
+ * @return The {@link SupportsDomains} if the topic supports domain operations.
+ * @throws UnsupportedOperationException If the topic does not support domain operations.
+ */
+ default SupportsDomains supportsDomains() {
+ throw new UnsupportedOperationException("Topic does not support domain operations.");
+ }
+
/**
* @return the {@link SupportsPolicies} if the topic supports policy operations.
* @throws UnsupportedOperationException if the topic does not support policy operations.
diff --git a/api/src/main/java/org/apache/gravitino/model/Model.java b/api/src/main/java/org/apache/gravitino/model/Model.java
index 73b00fc24f..dd25763723 100644
--- a/api/src/main/java/org/apache/gravitino/model/Model.java
+++ b/api/src/main/java/org/apache/gravitino/model/Model.java
@@ -24,6 +24,7 @@ import org.apache.gravitino.Auditable;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.Evolving;
import org.apache.gravitino.authorization.SupportsRoles;
+import org.apache.gravitino.domain.SupportsDomains;
import org.apache.gravitino.policy.SupportsPolicies;
import org.apache.gravitino.secret.SupportsSecrets;
import org.apache.gravitino.tag.SupportsTags;
@@ -86,6 +87,16 @@ public interface Model extends Auditable {
throw new UnsupportedOperationException("Model does not support tag operations.");
}
+ /**
+ * Returns the domain operations for this model.
+ *
+ * @return The {@link SupportsDomains} if the model supports domain operations.
+ * @throws UnsupportedOperationException If the model does not support domain operations.
+ */
+ default SupportsDomains supportsDomains() {
+ throw new UnsupportedOperationException("Model does not support domain operations.");
+ }
+
/**
* @return The {@link SupportsPolicies} if the model supports policy operations.
* @throws UnsupportedOperationException If the model does not support policy operations.
diff --git a/api/src/main/java/org/apache/gravitino/rel/Table.java b/api/src/main/java/org/apache/gravitino/rel/Table.java
index 0154811bfe..35acaf91cc 100644
--- a/api/src/main/java/org/apache/gravitino/rel/Table.java
+++ b/api/src/main/java/org/apache/gravitino/rel/Table.java
@@ -25,6 +25,7 @@ import org.apache.gravitino.Auditable;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.Evolving;
import org.apache.gravitino.authorization.SupportsRoles;
+import org.apache.gravitino.domain.SupportsDomains;
import org.apache.gravitino.policy.SupportsPolicies;
import org.apache.gravitino.rel.expressions.distributions.Distribution;
import org.apache.gravitino.rel.expressions.distributions.Distributions;
@@ -161,6 +162,16 @@ public interface Table extends Auditable {
throw new UnsupportedOperationException("Table does not support tag operations.");
}
+ /**
+ * Returns the domain operations for this table.
+ *
+ * @return The {@link SupportsDomains} if the table supports domain operations.
+ * @throws UnsupportedOperationException If the table does not support domain operations.
+ */
+ default SupportsDomains supportsDomains() {
+ throw new UnsupportedOperationException("Table does not support domain operations.");
+ }
+
/**
* @return The {@link SupportsPolicies} if the table supports policy operations.
* @throws UnsupportedOperationException If the table does not support policy operations.
diff --git a/api/src/main/java/org/apache/gravitino/rel/View.java b/api/src/main/java/org/apache/gravitino/rel/View.java
index e45396836e..c2a09d3795 100644
--- a/api/src/main/java/org/apache/gravitino/rel/View.java
+++ b/api/src/main/java/org/apache/gravitino/rel/View.java
@@ -25,6 +25,7 @@ import javax.annotation.Nullable;
import org.apache.gravitino.Auditable;
import org.apache.gravitino.Namespace;
import org.apache.gravitino.annotation.Unstable;
+import org.apache.gravitino.domain.SupportsDomains;
import org.apache.gravitino.secret.SupportsSecrets;
import org.apache.gravitino.semantic.SupportsSemanticModels;
import org.apache.gravitino.tag.SupportsTags;
@@ -135,6 +136,16 @@ public interface View extends Auditable {
throw new UnsupportedOperationException("View does not support tag operations.");
}
+ /**
+ * Returns the domain operations for this view.
+ *
+ * @return The {@link SupportsDomains} if the view supports domain operations.
+ * @throws UnsupportedOperationException If the view does not support domain operations.
+ */
+ default SupportsDomains supportsDomains() {
+ throw new UnsupportedOperationException("View does not support domain operations.");
+ }
+
/**
* @return the {@link SupportsSecrets} if the view supports secret property operations.
* @throws UnsupportedOperationException if the view does not support secret property operations.
diff --git a/api/src/main/java/org/apache/gravitino/semantic/SemanticModel.java b/api/src/main/java/org/apache/gravitino/semantic/SemanticModel.java
index 84cef9b560..bd5fc6b856 100644
--- a/api/src/main/java/org/apache/gravitino/semantic/SemanticModel.java
+++ b/api/src/main/java/org/apache/gravitino/semantic/SemanticModel.java
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
import org.apache.gravitino.Audit;
import org.apache.gravitino.Auditable;
import org.apache.gravitino.annotation.Evolving;
+import org.apache.gravitino.domain.SupportsDomains;
import org.apache.gravitino.policy.SupportsPolicies;
import org.apache.gravitino.tag.SupportsTags;
@@ -88,6 +89,16 @@ public interface SemanticModel extends Auditable {
throw new UnsupportedOperationException("Semantic Model does not support tag operations.");
}
+ /**
+ * Returns the domain operations for this Semantic Model.
+ *
+ * @return The {@link SupportsDomains} if the Semantic Model supports domain operations.
+ * @throws UnsupportedOperationException If the Semantic Model does not support domain operations.
+ */
+ default SupportsDomains supportsDomains() {
+ throw new UnsupportedOperationException("Semantic Model does not support domain operations.");
+ }
+
/**
* Returns the tag-derived policy lookup operations for this Semantic Model.
*
diff --git a/api/src/test/java/org/apache/gravitino/TestMetadataObjects.java b/api/src/test/java/org/apache/gravitino/TestMetadataObjects.java
index 95861fd7c6..b29551718f 100644
--- a/api/src/test/java/org/apache/gravitino/TestMetadataObjects.java
+++ b/api/src/test/java/org/apache/gravitino/TestMetadataObjects.java
@@ -128,6 +128,32 @@ public class TestMetadataObjects {
() -> MetadataObjects.of("parent", "template_abc", MetadataObject.Type.JOB_TEMPLATE));
}
+ @Test
+ public void testDomainObject() {
+ MetadataObject domainObject = MetadataObjects.of(null, "finance", MetadataObject.Type.DOMAIN);
+ Assertions.assertNull(domainObject.parent());
+ Assertions.assertEquals("finance", domainObject.name());
+ Assertions.assertEquals("finance", domainObject.fullName());
+ Assertions.assertEquals(MetadataObject.Type.DOMAIN, domainObject.type());
+ Assertions.assertNull(MetadataObjects.parent(domainObject));
+
+ MetadataObject domainObject2 = MetadataObjects.parse("finance", MetadataObject.Type.DOMAIN);
+ Assertions.assertEquals(domainObject, domainObject2);
+
+ MetadataObject domainObject3 =
+ MetadataObjects.of(Lists.newArrayList("finance"), MetadataObject.Type.DOMAIN);
+ Assertions.assertEquals(domainObject, domainObject3);
+
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> MetadataObjects.of("parent", "finance", MetadataObject.Type.DOMAIN));
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () ->
+ MetadataObjects.of(
+ Lists.newArrayList("parent", "finance"), MetadataObject.Type.DOMAIN));
+ }
+
@Test
public void testViewObject() {
MetadataObject viewObject =
diff --git a/api/src/test/java/org/apache/gravitino/domain/TestDomainChange.java b/api/src/test/java/org/apache/gravitino/domain/TestDomainChange.java
new file mode 100644
index 0000000000..ff39e341f8
--- /dev/null
+++ b/api/src/test/java/org/apache/gravitino/domain/TestDomainChange.java
@@ -0,0 +1,93 @@
+/*
+ * 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.gravitino.domain;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestDomainChange {
+
+ @Test
+ public void testRename() {
+ DomainChange change = DomainChange.rename("markets");
+ Assertions.assertInstanceOf(DomainChange.RenameDomain.class, change);
+ Assertions.assertEquals("markets", ((DomainChange.RenameDomain) change).getNewName());
+ Assertions.assertEquals("RENAMEDOMAIN markets", change.toString());
+
+ Assertions.assertEquals(DomainChange.rename("markets"), change);
+ Assertions.assertEquals(DomainChange.rename("markets").hashCode(), change.hashCode());
+ Assertions.assertNotEquals(DomainChange.rename("trading"), change);
+ }
+
+ @Test
+ public void testUpdateComment() {
+ DomainChange change = DomainChange.updateComment("new comment");
+ Assertions.assertInstanceOf(DomainChange.UpdateDomainComment.class, change);
+ Assertions.assertEquals(
+ "new comment", ((DomainChange.UpdateDomainComment) change).getNewComment());
+ Assertions.assertEquals("UPDATEDOMAINCOMMENT new comment", change.toString());
+
+ Assertions.assertEquals(DomainChange.updateComment("new comment"), change);
+ Assertions.assertEquals(
+ DomainChange.updateComment("new comment").hashCode(), change.hashCode());
+ Assertions.assertNotEquals(DomainChange.updateComment("other comment"), change);
+
+ // A null comment clears the comment and must not break equals or hashCode.
+ DomainChange clearComment = DomainChange.updateComment(null);
+ Assertions.assertNull(((DomainChange.UpdateDomainComment) clearComment).getNewComment());
+ Assertions.assertEquals(DomainChange.updateComment(null), clearComment);
+ Assertions.assertEquals(DomainChange.updateComment(null).hashCode(), clearComment.hashCode());
+ Assertions.assertNotEquals(change, clearComment);
+ }
+
+ @Test
+ public void testSetProperty() {
+ DomainChange change = DomainChange.setProperty("owner_team", "finance");
+ Assertions.assertInstanceOf(DomainChange.SetProperty.class, change);
+ DomainChange.SetProperty setProperty = (DomainChange.SetProperty) change;
+ Assertions.assertEquals("owner_team", setProperty.getProperty());
+ Assertions.assertEquals("finance", setProperty.getValue());
+ Assertions.assertEquals("SETDOMAINPROPERTY owner_team = finance", change.toString());
+
+ Assertions.assertEquals(DomainChange.setProperty("owner_team", "finance"), change);
+ Assertions.assertEquals(
+ DomainChange.setProperty("owner_team", "finance").hashCode(), change.hashCode());
+ Assertions.assertNotEquals(DomainChange.setProperty("owner_team", "hr"), change);
+ Assertions.assertNotEquals(DomainChange.setProperty("region", "finance"), change);
+ }
+
+ @Test
+ public void testRemoveProperty() {
+ DomainChange change = DomainChange.removeProperty("owner_team");
+ Assertions.assertInstanceOf(DomainChange.RemoveProperty.class, change);
+ Assertions.assertEquals("owner_team", ((DomainChange.RemoveProperty) change).getProperty());
+ Assertions.assertEquals("REMOVEDOMAINPROPERTY owner_team", change.toString());
+
+ Assertions.assertEquals(DomainChange.removeProperty("owner_team"), change);
+ Assertions.assertEquals(
+ DomainChange.removeProperty("owner_team").hashCode(), change.hashCode());
+ Assertions.assertNotEquals(DomainChange.removeProperty("region"), change);
+ }
+
+ @Test
+ public void testDifferentChangeTypesAreNotEqual() {
+ Assertions.assertNotEquals(DomainChange.rename("a"), DomainChange.updateComment("a"));
+ Assertions.assertNotEquals(DomainChange.removeProperty("a"), DomainChange.rename("a"));
+ }
+}
diff --git a/api/src/test/java/org/apache/gravitino/domain/TestDomainMemberFilter.java b/api/src/test/java/org/apache/gravitino/domain/TestDomainMemberFilter.java
new file mode 100644
index 0000000000..ed2b249268
--- /dev/null
+++ b/api/src/test/java/org/apache/gravitino/domain/TestDomainMemberFilter.java
@@ -0,0 +1,173 @@
+/*
+ * 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.gravitino.domain;
+
+import com.google.common.collect.ImmutableSet;
+import org.apache.gravitino.MetadataObject;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestDomainMemberFilter {
+
+ @Test
+ public void testDefaultFilter() {
+ DomainMemberFilter filter = DomainMemberFilter.builder().build();
+ Assertions.assertTrue(filter.types().isEmpty());
+ Assertions.assertFalse(filter.nameContains().isPresent());
+ Assertions.assertTrue(filter.domains().isEmpty());
+
+ Assertions.assertEquals(DomainMemberFilter.builder().build(), filter);
+ Assertions.assertEquals(DomainMemberFilter.builder().build().hashCode(), filter.hashCode());
+ }
+
+ @Test
+ public void testFilterWithAllFields() {
+ DomainMemberFilter filter =
+ DomainMemberFilter.builder()
+ .withTypes(MetadataObject.Type.TABLE, MetadataObject.Type.VIEW)
+ .withNameContains("trade")
+ .withDomains("finance", "tax")
+ .build();
+ Assertions.assertEquals(
+ ImmutableSet.of(MetadataObject.Type.TABLE, MetadataObject.Type.VIEW), filter.types());
+ Assertions.assertEquals("trade", filter.nameContains().get());
+ Assertions.assertEquals(ImmutableSet.of("finance", "tax"), filter.domains());
+
+ // Order and duplicates do not matter, and the setters are independent of each other.
+ DomainMemberFilter sameFilter =
+ DomainMemberFilter.builder()
+ .withDomains("tax", "finance", "tax")
+ .withNameContains("trade")
+ .withTypes(MetadataObject.Type.VIEW, MetadataObject.Type.TABLE)
+ .build();
+ Assertions.assertEquals(sameFilter, filter);
+ Assertions.assertEquals(sameFilter.hashCode(), filter.hashCode());
+ Assertions.assertNotEquals(DomainMemberFilter.builder().build(), filter);
+ Assertions.assertTrue(filter.toString().contains("domains=[finance, tax]"));
+ }
+
+ @Test
+ public void testDomains() {
+ // The scope of the listed domain itself, or of some of its sub-domains.
+ DomainMemberFilter financeScope = DomainMemberFilter.builder().withDomains("finance").build();
+ Assertions.assertEquals(ImmutableSet.of("finance"), financeScope.domains());
+ DomainMemberFilter taxScope = DomainMemberFilter.builder().withDomains("tax").build();
+ Assertions.assertEquals(ImmutableSet.of("tax"), taxScope.domains());
+ Assertions.assertNotEquals(financeScope, taxScope);
+
+ // No domain given: the whole scope of the listed domain, the same as the default.
+ Assertions.assertEquals(
+ DomainMemberFilter.builder().build(), DomainMemberFilter.builder().withDomains().build());
+ }
+
+ @Test
+ public void testLaterCallReplacesValue() {
+ DomainMemberFilter filter =
+ DomainMemberFilter.builder()
+ .withTypes(MetadataObject.Type.TABLE)
+ .withTypes(MetadataObject.Type.VIEW)
+ .withNameContains("trade")
+ .withNameContains("tax")
+ .withDomains("trading")
+ .withDomains("tax")
+ .build();
+ Assertions.assertEquals(ImmutableSet.of(MetadataObject.Type.VIEW), filter.types());
+ Assertions.assertEquals("tax", filter.nameContains().get());
+ Assertions.assertEquals(ImmutableSet.of("tax"), filter.domains());
+
+ // Setting no types or domains, or a null name, resets the option to its default.
+ DomainMemberFilter reset =
+ DomainMemberFilter.builder()
+ .withTypes(MetadataObject.Type.TABLE)
+ .withTypes()
+ .withNameContains("trade")
+ .withNameContains(null)
+ .withDomains("tax")
+ .withDomains()
+ .build();
+ Assertions.assertEquals(DomainMemberFilter.builder().build(), reset);
+ }
+
+ @Test
+ public void testCollectionsAreImmutable() {
+ DomainMemberFilter filter =
+ DomainMemberFilter.builder()
+ .withTypes(MetadataObject.Type.TABLE)
+ .withDomains("trading")
+ .build();
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> filter.types().add(MetadataObject.Type.VIEW));
+ Assertions.assertThrows(UnsupportedOperationException.class, () -> filter.domains().add("tax"));
+ }
+
+ @Test
+ public void testSupportedTypes() {
+ for (MetadataObject.Type type : DomainMember.SUPPORTED_TYPES) {
+ Assertions.assertEquals(
+ ImmutableSet.of(type), DomainMemberFilter.builder().withTypes(type).build().types());
+ }
+
+ for (MetadataObject.Type type : MetadataObject.Type.values()) {
+ if (!DomainMember.SUPPORTED_TYPES.contains(type)) {
+ IllegalArgumentException e =
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withTypes(MetadataObject.Type.TABLE, type));
+ Assertions.assertTrue(e.getMessage().contains(type.name()));
+ }
+ }
+
+ // Only leaf objects are members: never catalogs, schemas, columns, functions or domains.
+ Assertions.assertEquals(
+ ImmutableSet.of(
+ MetadataObject.Type.TABLE,
+ MetadataObject.Type.VIEW,
+ MetadataObject.Type.FILESET,
+ MetadataObject.Type.TOPIC,
+ MetadataObject.Type.MODEL,
+ MetadataObject.Type.SEMANTIC_MODEL),
+ DomainMember.SUPPORTED_TYPES);
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withTypes(MetadataObject.Type.CATALOG));
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withTypes(MetadataObject.Type.SCHEMA));
+ }
+
+ @Test
+ public void testInvalidFields() {
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withTypes((MetadataObject.Type[]) null));
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withTypes(MetadataObject.Type.TABLE, null));
+ Assertions.assertThrows(
+ IllegalArgumentException.class, () -> DomainMemberFilter.builder().withNameContains(" "));
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withDomains((String[]) null));
+ Assertions.assertThrows(
+ IllegalArgumentException.class, () -> DomainMemberFilter.builder().withDomains("tax", ""));
+ Assertions.assertThrows(
+ IllegalArgumentException.class,
+ () -> DomainMemberFilter.builder().withDomains("tax", null));
+ }
+}
diff --git a/api/src/test/java/org/apache/gravitino/domain/TestDomainRelation.java b/api/src/test/java/org/apache/gravitino/domain/TestDomainRelation.java
new file mode 100644
index 0000000000..609c007426
--- /dev/null
+++ b/api/src/test/java/org/apache/gravitino/domain/TestDomainRelation.java
@@ -0,0 +1,109 @@
+/*
+ * 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.gravitino.domain;
+
+import org.apache.gravitino.MetadataObject;
+import org.apache.gravitino.MetadataObjects;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestDomainRelation {
+
+ @Test
+ public void testDirect() {
+ DomainRelation relation = DomainRelation.direct();
+ Assertions.assertEquals(DomainRelation.Type.DIRECT, relation.type());
+ Assertions.assertFalse(relation.source().isPresent());
+ Assertions.assertEquals("DIRECT", relation.toString());
+ Assertions.assertEquals(DomainRelation.direct(), relation);
+ Assertions.assertEquals(DomainRelation.direct().hashCode(), relation.hashCode());
+ }
+
+ @Test
+ public void testInherited() {
+ MetadataObject schema = MetadataObjects.of("cat", "db", MetadataObject.Type.SCHEMA);
+ DomainRelation relation = DomainRelation.inherited(schema);
+ Assertions.assertEquals(DomainRelation.Type.INHERITED, relation.type());
+ Assertions.assertEquals(schema, relation.source().get());
+ Assertions.assertEquals("INHERITED cat.db", relation.toString());
+
+ MetadataObject catalog = MetadataObjects.of(null, "cat", MetadataObject.Type.CATALOG);
+ Assertions.assertEquals(catalog, DomainRelation.inherited(catalog).source().get());
+
+ Assertions.assertEquals(DomainRelation.inherited(schema), relation);
+ Assertions.assertEquals(DomainRelation.inherited(schema).hashCode(), relation.hashCode());
+ Assertions.assertNotEquals(DomainRelation.inherited(catalog), relation);
+ Assertions.assertNotEquals(DomainRelation.direct(), relation);
+ }
+
+ @Test
+ public void testInheritedNormalizesSource() {
+ // A source from another MetadataObject implementation, such as a DTO, keeps only its name and
+ // type, so relations built from it compare equal to those built from MetadataObjects.
+ MetadataObject other =
+ new MetadataObject() {
+ @Override
+ public String parent() {
+ return "cat";
+ }
+
+ @Override
+ public String name() {
+ return "db";
+ }
+
+ @Override
+ public MetadataObject.Type type() {
+ return MetadataObject.Type.SCHEMA;
+ }
+ };
+ MetadataObject schema = MetadataObjects.of("cat", "db", MetadataObject.Type.SCHEMA);
+ DomainRelation relation = DomainRelation.inherited(other);
+ Assertions.assertEquals(schema, relation.source().get());
+ Assertions.assertEquals(DomainRelation.inherited(schema), relation);
+ Assertions.assertEquals(DomainRelation.inherited(schema).hashCode(), relation.hashCode());
+ }
+
+ @Test
+ public void testInheritedNeedsCatalogOrSchema() {
+ Assertions.assertThrows(IllegalArgumentException.class, () -> DomainRelation.inherited(null));
+ MetadataObject table = MetadataObjects.of("cat.db", "trades", MetadataObject.Type.TABLE);
+ IllegalArgumentException e =
+ Assertions.assertThrows(
+ IllegalArgumentException.class, () -> DomainRelation.inherited(table));
+ Assertions.assertTrue(e.getMessage().contains("TABLE"));
+ }
+
+ @Test
+ public void testViaSubdomain() {
+ DomainRelation relation = DomainRelation.viaSubdomain("trading");
+ Assertions.assertEquals(DomainRelation.Type.VIA_SUBDOMAIN, relation.type());
+ Assertions.assertEquals(
+ MetadataObjects.of(null, "trading", MetadataObject.Type.DOMAIN), relation.source().get());
+ Assertions.assertEquals("VIA_SUBDOMAIN trading", relation.toString());
+
+ Assertions.assertEquals(DomainRelation.viaSubdomain("trading"), relation);
+ Assertions.assertEquals(DomainRelation.viaSubdomain("trading").hashCode(), relation.hashCode());
+ Assertions.assertNotEquals(DomainRelation.viaSubdomain("tax"), relation);
+
+ Assertions.assertThrows(IllegalArgumentException.class, () -> DomainRelation.viaSubdomain(""));
+ Assertions.assertThrows(
+ IllegalArgumentException.class, () -> DomainRelation.viaSubdomain(null));
+ }
+}
diff --git a/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java b/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java
index 7ca970d067..1acfd7460b 100644
--- a/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java
+++ b/api/src/test/java/org/apache/gravitino/exceptions/TestExceptions.java
@@ -43,4 +43,17 @@ public class TestExceptions {
Assertions.assertTrue(noCause instanceof GravitinoRuntimeException);
Assertions.assertTrue(withCause instanceof GravitinoRuntimeException);
}
+
+ @Test
+ public void testDomainExceptionsHierarchy() {
+ // The parents drive the HTTP mapping: AlreadyExistsException to 409, NotFoundException to 404.
+ Object alreadyBound = new BackingTagAlreadyBoundException("Tag %s already backs %s", "t", "d");
+ Object alreadyExists = new DomainAlreadyExistsException("Domain %s already exists", "d");
+ Object noSuchDomain = new NoSuchDomainException("Domain %s does not exist", "d");
+ Assertions.assertTrue(alreadyBound instanceof AlreadyExistsException);
+ Assertions.assertTrue(alreadyExists instanceof AlreadyExistsException);
+ Assertions.assertTrue(noSuchDomain instanceof NotFoundException);
+ Assertions.assertEquals(
+ "Tag t already backs d", ((BackingTagAlreadyBoundException) alreadyBound).getMessage());
+ }
}
diff --git a/core/src/main/java/org/apache/gravitino/utils/MetadataObjectUtil.java b/core/src/main/java/org/apache/gravitino/utils/MetadataObjectUtil.java
index 70af3cf295..30566a0a98 100644
--- a/core/src/main/java/org/apache/gravitino/utils/MetadataObjectUtil.java
+++ b/core/src/main/java/org/apache/gravitino/utils/MetadataObjectUtil.java
@@ -102,6 +102,16 @@ public class MetadataObjectUtil {
.orElseThrow(() -> new IllegalArgumentException("Unknown metadata object type: " + type));
}
+ /**
+ * Returns the entity types that metadata object types are mapped to. A metadata object type that
+ * is not backed by an entity yet has no entry here.
+ *
+ * @return The entity types of the metadata objects.
+ */
+ public static Set<Entity.EntityType> metadataObjectEntityTypes() {
+ return TYPE_TO_TYPE_MAP.values();
+ }
+
/**
* Convert the given {@link MetadataObject} full name to the corresponding {@link NameIdentifier}.
*
diff --git a/core/src/test/java/org/apache/gravitino/utils/TestMetadataObjectUtil.java b/core/src/test/java/org/apache/gravitino/utils/TestMetadataObjectUtil.java
index 7ecd04fa24..02abd359a8 100644
--- a/core/src/test/java/org/apache/gravitino/utils/TestMetadataObjectUtil.java
+++ b/core/src/test/java/org/apache/gravitino/utils/TestMetadataObjectUtil.java
@@ -28,6 +28,7 @@ import static org.mockito.Mockito.when;
import java.util.Collections;
import java.util.List;
+import java.util.Set;
import java.util.stream.Collectors;
import org.apache.gravitino.Entity;
import org.apache.gravitino.GravitinoEnv;
@@ -120,6 +121,23 @@ public class TestMetadataObjectUtil {
"catalog.schema", "sales_model", MetadataObject.Type.SEMANTIC_MODEL)));
}
+ @Test
+ public void testMetadataObjectEntityTypes() {
+ Set<Entity.EntityType> entityTypes = MetadataObjectUtil.metadataObjectEntityTypes();
+ for (MetadataObject.Type type : MetadataObject.Type.values()) {
+ if (type == MetadataObject.Type.DOMAIN) {
+ // DOMAIN is not backed by an entity yet.
+ Assertions.assertThrows(
+ IllegalArgumentException.class, () -> MetadataObjectUtil.toEntityType(type));
+ continue;
+ }
+ Assertions.assertTrue(entityTypes.contains(MetadataObjectUtil.toEntityType(type)));
+ }
+ Assertions.assertEquals(MetadataObject.Type.values().length - 1, entityTypes.size());
+ Assertions.assertThrows(
+ UnsupportedOperationException.class, () -> entityTypes.add(Entity.EntityType.USER));
+ }
+
@Test
public void testToEntityIdent() {
Assertions.assertThrows(
diff --git a/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java b/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java
index 3798da7492..4d184b6edc 100644
--- a/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java
+++ b/server-common/src/main/java/org/apache/gravitino/server/authorization/MetadataAuthzHelper.java
@@ -31,7 +31,6 @@ import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.function.Function;
-import java.util.stream.Collectors;
import org.apache.gravitino.Config;
import org.apache.gravitino.Configs;
import org.apache.gravitino.Entity;
@@ -90,9 +89,7 @@ public class MetadataAuthzHelper {
Arrays.asList(Entity.EntityType.TABLE, Entity.EntityType.TOPIC);
private static final Set<Entity.EntityType> METADATA_OBJECT_ENTITY_TYPES =
- Arrays.stream(MetadataObject.Type.values())
- .map(MetadataObjectUtil::toEntityType)
- .collect(Collectors.toUnmodifiableSet());
+ MetadataObjectUtil.metadataObjectEntityTypes();
private static final String TABLE_PARENT_SCOPES = "METALAKE, CATALOG, SCHEMA";
private static final String MODEL_PARENT_SCOPES = "METALAKE, CATALOG, SCHEMA";