Repository navigation
feature: Implement alias db lookups #712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ctor-polaris/src/main/java/com/netflix/metacat/connector/polaris/PolarisAliasService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| package com.netflix.metacat.connector.polaris; | ||
|
|
||
| import com.netflix.metacat.common.QualifiedName; | ||
| import com.netflix.metacat.common.server.properties.Config; | ||
| import com.netflix.metacat.common.server.usermetadata.AliasService; | ||
| import com.netflix.metacat.connector.polaris.store.repos.PolarisAliasRepository; | ||
| import lombok.NonNull; | ||
| import lombok.extern.slf4j.Slf4j; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Resolves table aliases against the polaris store. | ||
| */ | ||
| @Slf4j | ||
| public class PolarisAliasService implements AliasService { | ||
| private final PolarisAliasRepository aliasRepository; | ||
| private final Config config; | ||
|
|
||
| /** | ||
| * Constructor. | ||
| * | ||
| * @param aliasRepository the alias repository | ||
| * @param config the runtime config | ||
| */ | ||
| public PolarisAliasService(final PolarisAliasRepository aliasRepository, final Config config) { | ||
| this.aliasRepository = aliasRepository; | ||
| this.config = config; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| */ | ||
| @Override | ||
| public QualifiedName getTableName(@NonNull final QualifiedName tableAlias) { | ||
| return resolve(tableAlias).orElse(tableAlias); | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritDoc} | ||
| * | ||
| * <p>Overridden so that the answer comes from the same cached lookup as | ||
| * {@link #getTableName(QualifiedName)} rather than costing a second resolution. | ||
| */ | ||
| @Override | ||
| public boolean isAlias(@NonNull final QualifiedName name) { | ||
| return config.isTableAliasEnabled() && resolve(name).isPresent(); | ||
| } | ||
|
|
||
| private Optional<QualifiedName> resolve(final QualifiedName name) { | ||
| if (name.getType() != QualifiedName.Type.TABLE) { | ||
| return Optional.empty(); | ||
| } | ||
| try { | ||
| return aliasRepository | ||
| .findSourceTable(name.getCatalogName(), name.getDatabaseName(), name.getTableName()) | ||
| .map(table -> QualifiedName.ofTable( | ||
| table.getCatalogName(), table.getDbName(), table.getTblName())); | ||
| } catch (Exception e) { | ||
| log.error("Failed to resolve alias for {}", name, e); | ||
| throw new IllegalStateException("Failed to resolve alias for " + name, e); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
...rc/main/java/com/netflix/metacat/connector/polaris/store/entities/PolarisAliasEntity.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.netflix.metacat.connector.polaris.store.entities; | ||
|
|
||
| import lombok.AllArgsConstructor; | ||
| import lombok.Builder; | ||
| import lombok.EqualsAndHashCode; | ||
| import lombok.Getter; | ||
| import lombok.NoArgsConstructor; | ||
| import lombok.ToString; | ||
| import org.hibernate.annotations.GenericGenerator; | ||
| import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
|
||
| import jakarta.persistence.Basic; | ||
| import jakarta.persistence.Column; | ||
| import jakarta.persistence.Embedded; | ||
| import jakarta.persistence.Entity; | ||
| import jakarta.persistence.EntityListeners; | ||
| import jakarta.persistence.GeneratedValue; | ||
| import jakarta.persistence.Id; | ||
| import jakarta.persistence.Table; | ||
|
|
||
| /** | ||
| * Entity class for a table alias. | ||
| */ | ||
| @Getter | ||
| @AllArgsConstructor | ||
| @NoArgsConstructor | ||
| @Builder(toBuilder = true) | ||
| @EqualsAndHashCode | ||
| @Entity | ||
| @ToString(callSuper = true) | ||
| @Table(name = "ALIASES") | ||
| @EntityListeners(AuditingEntityListener.class) | ||
| public class PolarisAliasEntity { | ||
| @Basic | ||
| @Id | ||
| @GeneratedValue(generator = "uuid") | ||
| @GenericGenerator(name = "uuid", strategy = "uuid2") | ||
| @Column(name = "id", nullable = false, unique = true, updatable = false) | ||
| private String aliasId; | ||
|
|
||
| @Basic | ||
| @Column(name = "db_id", nullable = false, updatable = false) | ||
| private String dbId; | ||
|
|
||
| @Basic | ||
| @Column(name = "name", nullable = false, updatable = false) | ||
| private String name; | ||
|
|
||
| @Basic | ||
| @Column(name = "source_tbl_id", nullable = false) | ||
| private String sourceTblId; | ||
|
|
||
| @Embedded | ||
| private AuditEntity audit; | ||
| } |
36 changes: 36 additions & 0 deletions
36
...c/main/java/com/netflix/metacat/connector/polaris/store/repos/PolarisAliasRepository.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package com.netflix.metacat.connector.polaris.store.repos; | ||
|
|
||
| import com.netflix.metacat.connector.polaris.store.entities.PolarisAliasEntity; | ||
| import com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity; | ||
| import org.springframework.data.jpa.repository.JpaRepository; | ||
| import org.springframework.data.jpa.repository.Query; | ||
| import org.springframework.data.repository.query.Param; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * JPA repository implementation for storing PolarisAliasEntity. | ||
| */ | ||
| @Repository | ||
| public interface PolarisAliasRepository extends JpaRepository<PolarisAliasEntity, String> { | ||
|
|
||
| /** | ||
| * Resolves an alias to the table it points at. | ||
| * | ||
| * @param catalogName catalog the alias is defined in | ||
| * @param dbName database the alias is defined in | ||
| * @param aliasName the alias | ||
| * @return the source table, or empty if the name is not an alias | ||
| */ | ||
| @Query("SELECT t FROM PolarisAliasEntity a " | ||
| + "JOIN PolarisDatabaseEntity d ON d.dbId = a.dbId " | ||
| + "JOIN PolarisTableEntity t ON t.tblId = a.sourceTblId " | ||
| + "WHERE d.catalogName = :catalogName " | ||
| + "AND d.dbName = :dbName " | ||
| + "AND a.name = :aliasName") | ||
| Optional<PolarisTableEntity> findSourceTable( | ||
| @Param("catalogName") String catalogName, | ||
| @Param("dbName") String dbName, | ||
| @Param("aliasName") String aliasName); | ||
| } |
81 changes: 81 additions & 0 deletions
81
...-polaris/src/test/java/com/netflix/metacat/connector/polaris/PolarisAliasServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.netflix.metacat.connector.polaris; | ||
|
|
||
| import com.netflix.metacat.common.QualifiedName; | ||
| import com.netflix.metacat.common.server.properties.Config; | ||
| import com.netflix.metacat.connector.polaris.store.entities.PolarisTableEntity; | ||
| import com.netflix.metacat.connector.polaris.store.repos.PolarisAliasRepository; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.Mockito; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.mockito.Mockito.never; | ||
| import static org.mockito.Mockito.verify; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * Tests for alias resolution against the polaris store. | ||
| */ | ||
| public class PolarisAliasServiceTest { | ||
|
|
||
| private PolarisAliasRepository aliasRepository; | ||
| private Config config; | ||
|
|
||
| private PolarisAliasService service; | ||
| private QualifiedName alias; | ||
| private QualifiedName source; | ||
|
|
||
| @BeforeEach | ||
| public void setup() { | ||
| aliasRepository = Mockito.mock(PolarisAliasRepository.class); | ||
| config = Mockito.mock(Config.class); | ||
| when(config.isTableAliasEnabled()).thenReturn(true); | ||
| service = new PolarisAliasService(aliasRepository, config); | ||
| alias = QualifiedName.ofTable("prodhive", "db", "the_alias"); | ||
| source = QualifiedName.ofTable("prodhive", "db", "the_source"); | ||
| } | ||
|
|
||
| private void givenAlias(final String catalog, final String db, final String table) { | ||
| when(aliasRepository.findSourceTable("prodhive", "db", "the_alias")) | ||
| .thenReturn(Optional.of(PolarisTableEntity.builder() | ||
| .catalogName(catalog).dbName(db).tblName(table).build())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResolvesAliasToSourceTable() { | ||
| givenAlias("prodhive", "db", "the_source"); | ||
| assertThat(service.getTableName(alias)).isEqualTo(source); | ||
| assertThat(service.isAlias(alias)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReturnsInputWhenNotAnAlias() { | ||
| when(aliasRepository.findSourceTable("prodhive", "db", "the_alias")).thenReturn(Optional.empty()); | ||
| assertThat(service.getTableName(alias)).isEqualTo(alias); | ||
| assertThat(service.isAlias(alias)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResolvesAliasPointingAtAnotherDatabase() { | ||
| givenAlias("prodhive", "other_db", "the_source"); | ||
| assertThat(service.getTableName(alias)) | ||
| .isEqualTo(QualifiedName.ofTable("prodhive", "other_db", "the_source")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testNonTableNamesAreNotLookedUp() { | ||
| final QualifiedName database = QualifiedName.ofDatabase("prodhive", "db"); | ||
| assertThat(service.getTableName(database)).isEqualTo(database); | ||
| assertThat(service.isAlias(database)).isFalse(); | ||
| verify(aliasRepository, never()).findSourceTable("prodhive", "db", null); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDoesNotResolveAliasWhenAliasNotEnabled() { | ||
| givenAlias("prodhive", "other_db", "the_source"); | ||
| when(config.isTableAliasEnabled()).thenReturn(false); | ||
| assertThat(service.isAlias(QualifiedName.ofTable("prodhive", "other_db", "the_source"))).isFalse(); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like we aren't consistent in using uuid_generate_v4 vs gen_random_uuid, but that's preexisting