-
Notifications
You must be signed in to change notification settings - Fork 3k
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
fix(ingest/lookml): support view inheritance for fields #11148
Merged
hsheth2
merged 8 commits into
datahub-project:master
from
sid-acryl:ing-689-lookml-view-inheritance
Aug 21, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5df193b
include parent fields
sid-acryl 7cd7814
update doc
sid-acryl d0c42ec
Merge branch 'master' into ing-689-lookml-view-inheritance
sid-acryl 3d7deb9
address review comments
sid-acryl 11c2b6c
address review comment
sid-acryl 00b34fd
Merge branch 'master' into ing-689-lookml-view-inheritance
sid-acryl d3711e7
doc update
sid-acryl 2b52125
Merge branch 'master' into ing-689-lookml-view-inheritance
sid-acryl 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
3 changes: 3 additions & 0 deletions
3
metadata-ingestion/src/datahub/ingestion/source/looker/looker_constant.py
This file contains 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 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 |
---|---|---|
|
@@ -8,6 +8,11 @@ | |
find_view_from_resolved_includes, | ||
) | ||
from datahub.ingestion.source.looker.looker_config import LookerConnectionDefinition | ||
from datahub.ingestion.source.looker.looker_constant import ( | ||
DIMENSION_GROUPS, | ||
DIMENSIONS, | ||
MEASURES, | ||
) | ||
from datahub.ingestion.source.looker.looker_dataclasses import LookerViewFile | ||
from datahub.ingestion.source.looker.looker_file_loader import LookerViewFileLoader | ||
from datahub.ingestion.source.looker.lookml_config import ( | ||
|
@@ -23,6 +28,39 @@ | |
logger = logging.getLogger(__name__) | ||
|
||
|
||
def merge_parent_and_child_fields( | ||
child_fields: List[dict], parent_fields: List[dict] | ||
) -> List[Dict]: | ||
# Fetch the fields from the parent view, i.e., the view name mentioned in view.extends, and include those | ||
# fields in child_fields. This inclusion will resolve the fields according to the precedence rules mentioned | ||
# in the LookML documentation: https://cloud.google.com/looker/docs/reference/param-view-extends. | ||
|
||
# Create a map field-name vs field | ||
child_field_map: dict = {} | ||
for field in child_fields: | ||
assert ( | ||
NAME in field | ||
), "A lookml view must have a name field" # name is required field of lookml field array | ||
|
||
child_field_map[field[NAME]] = field | ||
|
||
for field in parent_fields: | ||
assert ( | ||
NAME in field | ||
), "A lookml view must have a name field" # name is required field of lookml field array | ||
|
||
if field[NAME] in child_field_map: | ||
# Fields defined in the child view take higher precedence. | ||
# This is an override case where the child has redefined the parent field. | ||
# There are some additive attributes; however, we are not consuming them in metadata ingestion | ||
# and hence not adding them to the child field. | ||
continue | ||
|
||
child_fields.append(field) | ||
|
||
return child_fields | ||
|
||
|
||
class LookerFieldContext: | ||
raw_field: Dict[Any, Any] | ||
|
||
|
@@ -248,23 +286,21 @@ def resolve_extends_view_name( | |
) | ||
return None | ||
|
||
def get_including_extends( | ||
def _get_parent_attribute( | ||
self, | ||
field: str, | ||
attribute_name: str, | ||
) -> Optional[Any]: | ||
""" | ||
Search for the attribute_name in the parent views of the current view and return its value. | ||
""" | ||
extends = list( | ||
itertools.chain.from_iterable( | ||
self.raw_view.get("extends", self.raw_view.get("extends__all", [])) | ||
) | ||
) | ||
|
||
# First, check the current view. | ||
if field in self.raw_view: | ||
return self.raw_view[field] | ||
|
||
# The field might be defined in another view and this view is extending that view, | ||
# so we resolve this field while taking that into account. | ||
# following Looker's precedence rules. | ||
# Following Looker's precedence rules. | ||
# reversed the view-names mentioned in `extends` attribute | ||
for extend in reversed(extends): | ||
assert extend != self.raw_view[NAME], "a view cannot extend itself" | ||
extend_view = self.resolve_extends_view_name( | ||
|
@@ -275,8 +311,33 @@ def get_including_extends( | |
f"failed to resolve extends view {extend} in view {self.raw_view[NAME]} of" | ||
f" file {self.view_file.absolute_file_path}" | ||
) | ||
if field in extend_view: | ||
return extend_view[field] | ||
if attribute_name in extend_view: | ||
return extend_view[attribute_name] | ||
|
||
return None | ||
|
||
def get_including_extends( | ||
self, | ||
field: str, | ||
) -> Optional[Any]: | ||
|
||
# According to Looker's inheritance rules, we need to merge the fields(i.e. dimensions, measures and | ||
# dimension_groups) from both the child and parent. | ||
if field in [DIMENSIONS, DIMENSION_GROUPS, MEASURES]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a comment about why this is the case - certain fields are merged by extends, whereas others are overridden There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
# Get the child fields | ||
child_fields = self._get_list_dict(field) | ||
# merge parent and child fields | ||
return merge_parent_and_child_fields( | ||
child_fields=child_fields, | ||
parent_fields=self._get_parent_attribute(attribute_name=field) or [], | ||
) | ||
else: | ||
# Return the field from the current view if it exists. | ||
if field in self.raw_view: | ||
return self.raw_view[field] | ||
|
||
# The field might be defined in another view, and this view is extending that view, | ||
return self._get_parent_attribute(field) | ||
|
||
return None | ||
|
||
|
@@ -383,13 +444,13 @@ def _get_list_dict(self, attribute_name: str) -> List[Dict]: | |
return [] | ||
|
||
def dimensions(self) -> List[Dict]: | ||
return self._get_list_dict("dimensions") | ||
return self.get_including_extends(field=DIMENSIONS) or [] | ||
|
||
def measures(self) -> List[Dict]: | ||
return self._get_list_dict("measures") | ||
return self.get_including_extends(field=MEASURES) or [] | ||
|
||
def dimension_groups(self) -> List[Dict]: | ||
return self._get_list_dict("dimension_groups") | ||
return self.get_including_extends(field=DIMENSION_GROUPS) or [] | ||
|
||
def is_materialized_derived_view(self) -> bool: | ||
for k in self.derived_table(): | ||
|
@@ -433,7 +494,7 @@ def is_sql_based_derived_case(self) -> bool: | |
return False | ||
|
||
def is_native_derived_case(self) -> bool: | ||
# It is pattern 5 | ||
# It is pattern 5, mentioned in Class documentation | ||
if ( | ||
"derived_table" in self.raw_view | ||
and "explore_source" in self.raw_view["derived_table"] | ||
|
@@ -443,7 +504,7 @@ def is_native_derived_case(self) -> bool: | |
return False | ||
|
||
def is_sql_based_derived_view_without_fields_case(self) -> bool: | ||
# Pattern 6 | ||
# Pattern 6, mentioned in Class documentation | ||
fields: List[Dict] = [] | ||
|
||
fields.extend(self.dimensions()) | ||
|
This file contains 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
16 changes: 16 additions & 0 deletions
16
...a-ingestion/tests/integration/lookml/vv-lineage-and-liquid-templates/child_view.view.lkml
This file contains 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,16 @@ | ||
include: "parent_view.view.lkml" | ||
|
||
view: child_view { | ||
extends: [parent_view] | ||
|
||
dimension: id { | ||
primary_key: yes | ||
type: integer | ||
sql: ${TABLE}.id ;; | ||
} | ||
|
||
dimension: child_dimension_1 { | ||
type: string | ||
sql: ${TABLE}.child_dimension_1 ;; | ||
} | ||
} |
This file contains 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
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.
add a comment for this method?
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.
done