How to use Extends in LookML?🤔💡
LookML offers powerful tools for building scalable and maintainable data models. One of the important functionalities extends
plays a crucial role in achieving modularity and code re-use. This blog dives deep into these concepts and best practices and provides practical examples to empower you to write efficient LookML.
Let’s understand the fundamentals.
Extends allow you to re-use your code by creating copies of LookML objects that can then be integrated into other LookML objects and modified independently from the original LookML object.
In Looker, you can extend views, Explores, and LookML-defined dashboards. Modularizing your code extends allows you to treat pieces of code as plug-and-play units that you can then build upon to expand your model.
This offers more granular control. The extending definition takes precedence over the source in case of conflicts. From a business perspective, this is very practical because you can have one centralized code base that is re-used by multiple teams that can extend the core code and customize it for their own needs.
Certain Best practices to keep in mind(all the time)
- Clear Organization: Structure your LookML files logically—group related definitions (dimensions, measures, etc.) for efficient management and extension.
- Minimize Conflicts: Strive for minimal overlap between source files used with
merge
orextends
. This reduces the likelihood of conflicts and simplifies maintenance. - Document Extensions: Document the purpose and modifications made in extended objects. This enhances clarity and facilitates future understanding.
Extending Views
This is commonly done to add more fields and/or update logic to the existing fields.
Extending Explores
You may have multiple tables that must always be joined together, especially in a more normalized database architecture. To avoid rewriting the same joins repeatedly, you can make a “base” Explore that already joins them together and then extend it to create additional Explores that need to join in more views. Or you may need the same set of joined views, but with the new Explore starting from a different base view.
Now, let’s get your hands dirty.
In this blog, we will extend a view. See the LookML for our base view, the Customer view:
view: customer {
suggestions: yes
dimension: name {
sql: ${TABLE}.name ;;
}
dimension: status {
sql: ${TABLE}.status ;;
type: number
}
}
Here is the LookML for the Customer with Age Extensions view, which extends the Customer view:
include: "/views/customer.view"
view: customer_with_age_extensions {
extends: [customer]
suggestions: no
dimension: age {
type: number
sql: ${TABLE}.age ;;
}
dimension: status {
type: string
}
}
When you utilize Extends in LookML, Looker performs the following steps to create the extended object:
- Object Duplication: Looker creates a complete copy of the LookML for the object being extended (explore, view, or dashboard). This copy serves as the foundation for the extended object.
- Definition Merging: Looker then merges the LookML definitions from both the original object and the newly created extended object. This means the extended object inherits all dimensions, metrics, and settings from the original object.
- Conflict Resolution: During the merge, Looker identifies and resolves any potential conflicts between definitions in the two LookML sources. Here’s how conflicts are handled:
- Overriding Definitions: In most cases, if an element (dimension, metric, parameter) is defined in both the original object and the extended object, the definition within the extended object takes precedence. This allows for customization within the extended explore.
- Combining Parameters: However, for specific LookML parameters (like sql_table_name), Looker might combine values rather than override them entirely.
- LookML Interpretation: Once all conflicts are resolved and the LookML for the extended object is finalized, Looker interprets it using its standard logic. This means Looker applies all the default settings and assumptions it typically uses for explores, views, or dashboards.
To summarize
By mastering extends
, you can write modular, maintainable, and scalable LookML models. Remember to prioritize clear organization, conflict mitigation, and documentation for optimal results. With these best practices in mind, you can leverage the power of LookML to build robust and efficient data exploration experiences.