本页介绍的是
explore_source参数,它是derived_table的子参数。
explore_source也可以是test的子参数,如test参数文档页面中所述。
用法
derived_table: customer_order_facts {
explore_source: orders {
column: customer_id {
field: orders.customer_id
}
column: order_amount {
field: orders.sale_price
}
column: item_qty {
field: orders.number_items
}
derived_column: average_item_price {
sql: order_amount / item_qty ;;
}
timezone: "America/Los_Angeles"
}
}
|
层次结构
explore_source |
默认值
无
接受
原生派生表所派生自的“探索”的标识符,以及定义原生派生表的子参数。 |
定义
您可以通过两种方式创建派生表,并且可以像使用数据库中的普通表一样使用派生表:一种是原生派生表,使用 LookML 参数定义;另一种是基于 SQL 的派生表,使用 SQL 查询语句定义。
explore_source 参数用于 原生派生表。在此参数中,您可以定义原生派生表中将包含哪些列、要应用于原生派生表的任何过滤条件、是否限制原生派生表行或对其进行排序,以及是否将原生派生表基于时间的字段转换为其他时区。
定义原生派生表
您可以在原生派生表中使用各种参数,其中许多是可选的。定义原生派生表的语法如下:
explore_source: identifier {
bind_all_filters: yes
column: identifier {
field: field_name
}
derived_column: identifier {
sql: SQL expression ;;
}
expression_custom_filter: [custom filter expression]
filters: [field_name_1: "string", field_name_2: "string", ...]
limit: number
sorts: [field_name_1: asc | desc, field_name_2: asc | desc, ...]
timezone: "string"
}
下表提供了可用于定义原生派生表的每个参数的相关信息:
| 参数名称 | 说明 | 示例 |
|---|---|---|
bind_filters | 将过滤条件从“探索”查询传递到原生派生表子查询。如需设置此参数,请使用 from_field 子参数指定在原生派生表视图中定义的字段,或在原生派生表所联接的“探索”中可访问的字段。在运行时,对“探索”中的 from_field 的任何过滤条件都将传递到原生派生表子查询中的 to_field。如需查看示例,请参阅使用 bind_filters 部分。
使用 bind_filters 时,请注意以下事项:
explore_source 参数可以具有 bind_all_filters 子参数或 bind_filters 子参数,但不能同时具有这两个子参数。
|
bind_filters: {
to_field: users.created_date
from_field: user_dt.filter_date
} |
bind_all_filters |
将所有过滤条件从“探索”查询传递到原生派生表子查询。如需设置此参数,请在原生派生表的 explore_source 中指定 bind_all_filters: yes。如需查看示例,请参阅使用 bind_filters 部分。
使用 bind_all_filters: yes 时,请注意以下事项:
|
bind_all_filters: yes |
column |
指定要包含在 explore_source 中的列。具有 field 子参数。 |
column: cust_id {
field: orders.customer_id
} |
derived_column |
指定 explore_source 中的列,该列在内部列的命名空间中包含表达式。汇总 SQL 表达式在此处不起作用,因为此步骤中没有 SQL 分组。SQL 窗口函数在此参数中非常有用。具有 sql 子参数。 |
derived_column: average_order {
sql: order_amount / item_qty ;;
} |
dev_filters |
添加于 21.12
指定 Looker 仅应用于派生表的开发版本的过滤条件。当 LookML 开发者在 开发模式下测试派生表时,此参数非常有用。dev_filters 参数可让 Looker 构建较小的过滤表版本,以便 LookML 开发者可以迭代和测试表,而无需在每次更改后等待构建完整表。Looker 仅将 dev_filters 应用于派生表的开发版本,而不应用于用户查询的表的生产版本。如需详细了解如何在开发模式下使用派生表,请参阅 Looker 中的派生表文档页面;如需查看示例,请参阅本页面的为开发模式创建过滤条件部分。 |
dev_filters: [orders.created_date: "90 days", orders.products: "sweaters"] |
expression_custom_filter |
(可选)指定 自定义过滤表达式 在 explore_source 查询上。 |
expression_custom_filter: ${orders.status} = "pending" ;; |
filters |
(可选)向 explore_source 查询添加过滤条件。用方括号括起来;添加要过滤的字段名称,使用 view_name.field_name 格式,后跟 : 和字段应过滤的值。过滤条件会添加到原生派生表生成的 SQL 的 WHERE 子句中。 |
filters: [products.department: "sweaters"] |
limit |
(可选)指定查询的行数上限。 | limit: 10 |
sorts |
(可选)为此 explore_source 指定排序方式。用方括号括起来;添加要排序的字段名称,使用 view_name.field_name 格式,后跟 : 和 asc 或 desc,以指明字段应按升序还是降序排序。您可以通过添加多个字段名称和关键字对(以英文逗号分隔)来对多个字段进行排序。 |
sorts: [products.brand: asc, products.name: asc] |
timezone |
为 explore_source 查询设置时区。对于非永久性派生表,请将时区设置为 "query_timezone",以自动使用当前运行的查询的时区。如果未指定时区,explore_source 查询将不会执行任何时区转换,而是使用 数据库时区。如需查看支持的时区列表,请参阅 timezone 值页面。 在 IDE 中输入 timezone 参数时,IDE 会自动建议时区值。IDE 还会将支持的时区值列表显示在“快捷帮助”面板中。 |
timezone: "America/Los_Angeles" |
示例
以下定义提供了原生派生表的基本示例。
创建 user_order_facts 原生派生表:
view: user_order_facts {
derived_table: {
explore_source: order_items {
column: user_id {
field: order_items.user_id
}
column: lifetime_number_of_orders {
field: order_items.order_count
}
column: lifetime_customer_value {
field: order_items.total_revenue
}
}
}
# Define the view's fields as desired
dimension: user_id {
hidden: yes
}
dimension: lifetime_number_of_orders {
type: number
}
dimension: lifetime_customer_value {
type: number
}
}
您可以添加过滤条件来创建 user_90_day_facts 原生派生表:
view: user_90_day_facts {
derived_table: {
explore_source: order_items {
column: user_id {
field: order_items.user_id
}
column: number_of_orders_90_day {
field: order_items.order_count
}
column: customer_value_90_day {
field: order_items.total_revenue
}
filters: [order_items.created_date: "90 days"]
}
}
# Add define view's fields as desired
dimension: user_id {
hidden: yes
}
dimension: number_of_orders_90_day {
type: number
}
dimension: customer_value_90_day {
type: number
}
}
为开发模式创建过滤条件
在某些情况下,您创建的原生派生表需要很长时间才能生成,如果您在开发模式下测试大量更改,这可能会非常耗时。对于这些情况,您可以使用 dev_filters 创建较小的原生派生表开发版本:
view: e_faa_pdt {
derived_table: {
...
datagroup_trigger: e_faa_shared_datagroup
explore_source: flights {
dev_filters: [flights.event_date: "90 days"]
filters: [flights.event_date: "2 years", flights.airport_name: "Yucca Valley Airport"]
column: id {}
column: airport_name {}
column: event_date {}
}
}
...
}
此示例包含一个 dev_filters 参数,该参数将数据过滤为最近 90 天的数据;还包含一个 filters 参数,该参数将数据过滤为最近 2 年的数据和 Yucca Valley Airport 的数据。dev_filters 参数与 filters 参数结合使用,以便将所有过滤条件应用于表的开发版本。如果 dev_filters 和 filters 都为同一列指定了过滤条件,则 dev_filters 优先用于表的开发版本。在此示例中,表的开发版本会将 Yucca Valley Airport 的数据过滤为最近 90 天的数据。
如果原生派生表具有
请注意,反之亦然:如果您有一个具有dev_filters参数,则开发表不能用作生产版本,因为开发版本的数据集是缩略版。如果是这种情况,在您完成表的开发并部署更改之前,可以注释掉dev_filters参数,然后在开发模式下查询表。Looker 随后会构建一个完整版本的表,您可以在部署更改时将其用于生产。如需详细了解如何在生产中使用开发表,请参阅 Looker 中的派生表文档页面。dev_filters参数的原生派生表,并且您在开发模式下查询该表,Looker 可以使用生产表来回答您的开发模式查询。除非您更改表的定义,然后在开发模式下查询表,否则此语句为 true;在这种情况下,Looker 将构建一个开发表来回答查询。
将过滤条件传递到原生派生表
在“探索”中添加原生派生表时,您可以从“探索”中获取运行时过滤条件,并将其应用于原生派生表查询。为此,请将 bind_all_filters 或 bind_filters 参数添加到原生派生表的 explore_source 中。
将运行时过滤条件从“探索”传递到原生派生表子查询时,运行时过滤条件必须位于与原生派生表联接到同一“探索”的视图中。此外,由于原生派生表必须在运行时重新生成,才能纳入“探索”中的运行时过滤条件,因此原生派生表不能是永久性派生表 (PDT)。
使用 bind_all_filters
如需将过滤条件从“探索”传递到原生派生表子查询,最简单的方法莫过于在原生派生表的 explore_source 参数中指定 bind_all_filters: yes。这会将“探索”的所有运行时过滤条件传递到原生派生表子查询。
具有 bind_all_filters: yes 的原生派生表必须联接到原生派生表的 explore_source 参数中指定的同一“探索”。如果您想在其他“探索”中使用原生派生表,请改用使用 Using bind_filters 部分所述的 bind_filters 参数。
以下是具有 bind_all_filters: yes 的原生派生表的 LookML:
view: top_10_simple_item_names {
view_label: "Top 10s"
derived_table: {
explore_source: order_items {
column: total_sale_price {
field: order_items.total_sale_price
}
column: item_name {
field: products.item_name
}
derived_column: rank {
sql: RANK() OVER (ORDER BY total_sale_price DESC) ;;
}
bind_all_filters: yes
sorts: [order_items.total_sale_price: desc]
timezone: "query_timezone"
limit: 10
}
}
dimension: item_name {
group_label: "Simple Example"
}
dimension: rank {
type: number
group_label: "Simple Example"
}
dimension: item_name_ranked {
group_label: "Simple Example"
order_by_field: rank
type: string
sql: ${rank} || ') ' || ${item_name} ;;
}
}
在原生派生表的视图中,explore_source 为 order_items。以下是 order_items“探索”的 LookML,其中原生派生表联接到“探索”中:
explore: order_items {
...
join: top_10_simple_item_names {
type: inner
relationship: many_to_one
sql_on: ${products.item_name} = ${top_10_simple_item_names.item_name} ;;
}
}
如需查看此示例的实际效果,请参阅 [Analytic Block] – Pivot by Top X – Introducing bind_all_filters: yes 社区帖子。
使用 bind_filters
explore_source 的 bind_filters 子参数会将“探索”查询中的特定过滤条件传递到原生派生表子查询中:
to_field是作为过滤条件应用目标的原生派生表中的字段。to_field必须是底层explore_source中的字段。from_field用于指定从“探索”中获取过滤条件的字段(如果用户在运行时指定了过滤条件)。
在运行时,对“探索”中的 from_field 的任何过滤条件都将传递到原生派生表子查询中的 to_field。
以下 LookML 显示了具有 bind_filters 的原生派生表。通过此设置,Looker 将获取应用于“探索”中的 filtered_lookml_dt.filter_date 字段的任何过滤条件,并将该过滤条件应用于原生派生表中的 users.created_date 字段。
derived_table: {
explore_source: order_items {
bind_filters: {
to_field: users.created_date
from_field: filtered_lookml_dt.filter_date
. . .
}
}
}
注意事项
仅使用一个 explore_source 参数
每个原生派生表仅接受一个 explore_source 参数。后续的 explore_source 参数不会引发 LookML 验证错误,但会替换之前的 explore_source 参数。
如需根据不同视图中的字段创建列,请先在同一“探索”中联接不同的视图。然后将该“探索”用于 explore_source。
使用 include 语句启用对字段的引用
创建原生派生表时,您必须添加包含 “探索”定义的文件。最好的方法是在单独的“探索”文件中定义“探索”,如创建“探索”文件文档中所述。
如果您创建了单独的“探索”文件:
- 原生派生表的视图文件应包含“探索”的文件。例如:
-
include: "/explores/order_items.explore.lkml"
-
- “探索”的文件应包含所需的视图文件。例如:
-
include: "/views/order_items.view.lkml" -
include: "/views/users.view.lkml"
-
- 模型应包含“探索”的文件。例如:
-
include: "/explores/order_items.explore.lkml"
-