-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Fix table column tag hydration #27526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
PRADDZY
wants to merge
1
commit into
open-metadata:main
Choose a base branch
from
PRADDZY:hackathon/fix-table-column-tags
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+161
−10
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
147 changes: 147 additions & 0 deletions
147
...a-service/src/test/java/org/openmetadata/service/jdbi3/TableRepositoryColumnTagsTest.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,147 @@ | ||
| package org.openmetadata.service.jdbi3; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.mockito.ArgumentMatchers.anyList; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.UUID; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.openmetadata.schema.api.services.CreateDatabaseService; | ||
| import org.openmetadata.schema.entity.data.Table; | ||
| import org.openmetadata.schema.type.Column; | ||
| import org.openmetadata.schema.type.ColumnDataType; | ||
| import org.openmetadata.schema.type.EntityReference; | ||
| import org.openmetadata.schema.type.TagLabel; | ||
| import org.openmetadata.schema.type.TagLabel.LabelType; | ||
| import org.openmetadata.schema.type.TagLabel.State; | ||
| import org.openmetadata.schema.type.TagLabel.TagSource; | ||
| import org.openmetadata.schema.type.Include; | ||
| import org.openmetadata.service.Entity; | ||
| import org.openmetadata.service.util.EntityUtil.Fields; | ||
| import org.openmetadata.service.util.EntityUtil.RelationIncludes; | ||
| import org.openmetadata.service.util.FullyQualifiedName; | ||
|
|
||
| class TableRepositoryColumnTagsTest { | ||
| private CollectionDAO collectionDAO; | ||
| private CollectionDAO.TagUsageDAO tagUsageDAO; | ||
| private TableRepository repository; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| Entity.cleanup(); | ||
|
|
||
| collectionDAO = mock(CollectionDAO.class); | ||
| tagUsageDAO = mock(CollectionDAO.TagUsageDAO.class); | ||
|
|
||
| when(collectionDAO.tableDAO()).thenReturn(mock(CollectionDAO.TableDAO.class)); | ||
| when(collectionDAO.tagUsageDAO()).thenReturn(tagUsageDAO); | ||
| Entity.setCollectionDAO(collectionDAO); | ||
|
|
||
| repository = new TableRepository(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| Entity.cleanup(); | ||
| } | ||
|
|
||
| @Test | ||
| void setFields_hydratesColumnTagsWhenOnlyColumnsAreRequested() { | ||
| Table table = tableWithColumn("service.database.schema.users", "email"); | ||
| TagLabel piiTag = | ||
| new TagLabel() | ||
| .withTagFQN("PII.Sensitive") | ||
| .withSource(TagSource.CLASSIFICATION) | ||
| .withLabelType(LabelType.MANUAL) | ||
| .withState(State.CONFIRMED); | ||
| String columnFqn = table.getColumns().getFirst().getFullyQualifiedName(); | ||
|
|
||
| when(tagUsageDAO.getTagsByPrefix(table.getFullyQualifiedName(), ".%", true)) | ||
| .thenReturn(Map.of(FullyQualifiedName.buildHash(columnFqn), List.of(piiTag))); | ||
|
|
||
| repository.setFields( | ||
| table, | ||
| new Fields(repository.getAllowedFields(), "columns"), | ||
| RelationIncludes.fromInclude(Include.NON_DELETED)); | ||
|
|
||
| assertNull(table.getTags()); | ||
| assertColumnTags(table, piiTag); | ||
| } | ||
|
|
||
| @Test | ||
| void setFieldsInBulk_hydratesColumnTagsWhenOnlyColumnsAreRequested() { | ||
| Table table = tableWithColumn("service.database.schema.users", "email"); | ||
| TagLabel piiTag = | ||
| new TagLabel() | ||
| .withTagFQN("PII.Sensitive") | ||
| .withSource(TagSource.CLASSIFICATION) | ||
| .withLabelType(LabelType.MANUAL) | ||
| .withState(State.CONFIRMED); | ||
| String columnFqn = table.getColumns().getFirst().getFullyQualifiedName(); | ||
|
|
||
| when(tagUsageDAO.getTagsInternalBatch(anyList())) | ||
| .thenReturn(List.of(tagUsage(columnFqn, piiTag))); | ||
|
|
||
| repository.setFieldsInBulk( | ||
| new Fields(repository.getAllowedFields(), "columns"), List.of(table)); | ||
|
|
||
| assertNull(table.getTags()); | ||
| assertColumnTags(table, piiTag); | ||
| } | ||
|
|
||
| private static Table tableWithColumn(String tableFqn, String columnName) { | ||
| String schemaFqn = FullyQualifiedName.getParentFQN(tableFqn); | ||
| String databaseFqn = FullyQualifiedName.getParentFQN(schemaFqn); | ||
| String serviceFqn = FullyQualifiedName.getParentFQN(databaseFqn); | ||
| Column column = | ||
| new Column() | ||
| .withName(columnName) | ||
| .withFullyQualifiedName(FullyQualifiedName.add(tableFqn, columnName)) | ||
| .withDataType(ColumnDataType.STRING); | ||
|
|
||
| return new Table() | ||
| .withId(UUID.randomUUID()) | ||
| .withName(FullyQualifiedName.getShortName(tableFqn)) | ||
| .withFullyQualifiedName(tableFqn) | ||
| .withDatabaseSchema(entityReference(Entity.DATABASE_SCHEMA, schemaFqn)) | ||
| .withDatabase(entityReference(Entity.DATABASE, databaseFqn)) | ||
| .withService(entityReference(Entity.DATABASE_SERVICE, serviceFqn)) | ||
| .withServiceType(CreateDatabaseService.DatabaseServiceType.Mysql) | ||
| .withColumns(List.of(column)); | ||
| } | ||
|
|
||
| private static EntityReference entityReference(String type, String fqn) { | ||
| return new EntityReference() | ||
| .withId(UUID.randomUUID()) | ||
| .withType(type) | ||
| .withName(FullyQualifiedName.getShortName(fqn)) | ||
| .withFullyQualifiedName(fqn); | ||
| } | ||
|
|
||
| private static CollectionDAO.TagUsageDAO.TagLabelWithFQNHash tagUsage( | ||
| String targetFqn, TagLabel tagLabel) { | ||
| CollectionDAO.TagUsageDAO.TagLabelWithFQNHash usage = | ||
| new CollectionDAO.TagUsageDAO.TagLabelWithFQNHash(); | ||
| usage.setTargetFQNHash(FullyQualifiedName.buildHash(targetFqn)); | ||
| usage.setSource(tagLabel.getSource().ordinal()); | ||
| usage.setTagFQN(tagLabel.getTagFQN()); | ||
| usage.setLabelType(tagLabel.getLabelType().ordinal()); | ||
| usage.setState(tagLabel.getState().ordinal()); | ||
| return usage; | ||
| } | ||
|
|
||
| private static void assertColumnTags(Table table, TagLabel expectedTag) { | ||
| List<TagLabel> tags = table.getColumns().getFirst().getTags(); | ||
| assertEquals(1, tags.size()); | ||
| assertEquals(expectedTag.getTagFQN(), tags.getFirst().getTagFQN()); | ||
| assertEquals(expectedTag.getSource(), tags.getFirst().getSource()); | ||
| assertEquals(expectedTag.getLabelType(), tags.getFirst().getLabelType()); | ||
| assertEquals(expectedTag.getState(), tags.getFirst().getState()); | ||
| } | ||
| } | ||
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.
In
setFieldsInBulk_hydratesColumnTagsWhenOnlyColumnsAreRequested,assertNull(table.getTags())doesn’t prove bulk table-level tags were not fetched, sinceclearFieldsInternalwill null outtable.tagswheneverfieldsdoesn’t includetags(even if they were loaded). To fully regression-test the “keep top-level tags gated behind fields=tags” behavior, add a Mockito verification that the tag DAO is only queried for the column FQN(s) (and not also for the table FQN) whenFieldsis justcolumns.